I want a way of updating all fields in a Word 2013 document. (If it works in other versions, all the better; I originally had this problem with Word 2007, and nothing seems to have changed since then.) This includes cross-references, page numbers, tables of contents, indexes, headers, etc. If it can be updated by pressing F9, I want it updated.
(In theory updating fields can cause other fields to need updating, e.g. a longer table of contents changes some page numbers in the main text. Taking care of the common cases is good enough for me. In fact, it's ok if I have to run the macro two or three times before it stabilizes. I just want to have a single macro that finds everything.)
My attempt so far doesn't update fields in text boxes inside figures. How do I update them, and what else have I missed?
EDIT: Combining the answer given with what I already had gives a macro that seems to update everything (with a known defect).
'' 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