如何更新Word文檔中的所有欄位?

如何更新Word文檔中的所有欄位?

我想要一種更新方式全部Word 2013 文件中的欄位。 (如果它在其他版本中也能工作,那就更好了;我最初在Word 2007 中遇到了這個問題,從那時起似乎沒有任何改變。)這包括交叉引用、頁碼、目錄、索引、標題等。F9

(理論上,更新字段可能會導致其他字段需要更新,例如較長的​​目錄會更改正文中的某些頁碼。處理常見情況對我來說就足夠了。事實上,如果我必須運行也沒關係在它在穩定之前,我只需要一個能找到所有內容的巨集。

到目前為止,我的嘗試並未更新圖中文字方塊中的欄位。我該如何更新它們,我還錯過了什麼?


編輯:將給出的答案與我已有的答案結合,給出了一個似乎可以更新所有內容的巨集(使用已知缺陷)。

'' Update all the fields, indexes, etc. in the specified document.
Sub UpdateAllFieldsIn(doc As Document)
    '' Update tables. We do this first so that they contain all necessary
    '' entries and so extend to their final number of pages.
    Dim toc As TableOfContents
    For Each toc In doc.TablesOfContents
        toc.Update
    Next toc
    Dim tof As TableOfFigures
    For Each tof In doc.TablesOfFigures
        tof.Update
    Next tof
    '' Update fields everywhere. This includes updates of page numbers in
    '' tables (but would not add or remove entries). This also takes care of
    '' all index updates.
    Dim sr As range
    For Each sr In doc.StoryRanges
        sr.Fields.Update
        While Not (sr.NextStoryRange Is Nothing)
            Set sr = sr.NextStoryRange
            '' FIXME: for footnotes, endnotes and comments, I get a pop-up
            '' "Word cannot undo this action. Do you want to continue?"
            sr.Fields.Update
        Wend
    Next sr
End Sub
'' Update all the fields, indexes, etc. in the active document.
'' This is a parameterless subroutine so that it can be used interactively.
Sub UpdateAllFields()
    UpdateAllFieldsIn ActiveDocument
End Sub

答案1

進入列印設置,選擇更新字段。然後去列印,或列印預覽您的文件。

瞧,所有欄位都已更新!

Mac 2016 Word 中的 MS Word 列印選項

答案2

我只需執行Ctrl+ A- 選擇所有內容 - 並且然後 F9更新批次。

雖然,這會錯過頁首和頁腳,但它們會在您列印/列印預覽 IIRC 時更新。


###更新

我找到了以下巨集。在快速測試中,它更新了目錄、段落中的欄位、頁首和頁尾中的欄位以及浮動文字方塊圖中的欄位。

希望這涵蓋了您需要的所有內容,如果沒有,請指出仍然無法更新的內容。

來源:http://www.gmayor.com/installing_macro.htm

Sub UpdateAll()
    Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                oStory.Fields.Update
            Wend
        End If
    Next oStory
    Set oStory = Nothing
End Sub

答案3

看起來很有趣:

如果您使用的是 Word 2007,則流程略有不同:按一下 Office 按鈕,然後按一下 Word 選項。 Word 顯示「Word 選項」對話方塊。按一下對話方塊左側的“進階”。 (按此處查看相關圖。)在「常規」區域(向下捲動一點即可看到),請確保勾選「開啟時更新自動連結」複選框。按一下“確定”。該設定應確保您的所有連結始終是最新的。如果您想在開啟文件時更新字段,則需要使用巨集來完成該任務。具體來說,您需要使用 AutoOpen 或 AutoClose 宏,具體取決於您是否要在文件開啟或關閉時更新欄位。以下是您可以使用的 AutoOpen 巨集的範例。

Sub AutoOpen()
    With Options
        .UpdateFieldsAtPrint = True
        .UpdateLinksAtPrint = True
    End With
    ActiveDocument.Fields.Update
End Sub

請注意,巨集確保將選項設定為在列印時強制更新欄位和鏈接,然後更新文件中 Fields 集合的所有成員。相反,如果您想在關閉時更新字段,則可以使用以下巨集:

Sub AutoClose()
    ActiveDocument.Fields.Update
End Sub

該巨集要短得多,因為退出文件時無需設定列印時更新選項。

答案4

如果您想正確更新所有頁首和頁腳,這對我有用:

    Dim oStory As Range
    Dim oSection As Object
    Dim oHeader As Object
    Dim oFooter As Object

    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
    Next oStory

        For Each oSection In ActiveDocument.Sections
             For Each oHeader In oSection.Headers
                 oHeader.Range.Fields.Update
             Next oHeader
            
             For Each oFooter In oSection.Footers
                 oFooter.Range.Fields.Update
             Next oFooter
        Next oSection

相關內容