我想要一种更新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
Dim toa As Word.TableOfAuthorities / For Each toa In ActiveDocument.TablesOfAuthorities / toa.Update / Next