Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want a way of updating all fields in a Word 2007 document (if it works in other versions, all the better). 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
share|improve this question
    
For completeness you may also want to add the table of authorities: Dim toa As Word.TableOfAuthorities / For Each toa In ActiveDocument.TablesOfAuthorities / toa.Update / Next –  Terrance Feb 14 '13 at 19:29

3 Answers 3

up vote 22 down vote accepted

I just do Ctrl+A - to select everything - and then F9 to update the lot.

Although, this misses headers and footers, but they update when you print/print-preview IIRC.


Update

I've found the following macro. On a quick test it updated tables of contents, fields within paragraphs, fields within the header and footer, and fields within a floating text box figure.

Hopefully that covers everything that you need, if not please indicate what is still failing to update.

Source: 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
share|improve this answer
    
@Giles - OK, fair enough, always best to check that the basics have been explored first. I've just now had a hunt and found a macro that seems to do the job, check my update, let me know if it misses anything. –  DMA57361 Oct 7 '10 at 8:10
    
Now we're talking! I have no idea why iterating with NextStoryRange and with document.StoryRanges are different things, but your code combined with the table updates I already had make up a winner (well, almost, but that's a different issue). –  Gilles Oct 7 '10 at 19:02

Word 2010:

Right click the ribbon, customise the ribbon, choose command from "all commands" search for "update" and add it where you want.

This button updates only selected fields.
Then, to update all fields, press Ctrl + A then this button.

share|improve this answer
    
How does it differ from pressing F9? Does that really update inside figures, tables, etc? –  Gilles Jul 18 '13 at 19:44

This page looks interesting:

If you are using Word 2007, the process is a bit different: Click the Office button and then click Word Options. Word displays the Word Options dialog box. Click on Advanced at the left side of the dialog box. (Click here to see a related figure.) In the General area (scroll down a bit to see it), make sure the Update Automatic Links at Open check box is selected. Click on OK. That setting should make sure that all your links are always up to date. If you want to update the fields when the document is opened, you'll need to use a macro to accomplish the task. Specifically, you'll need to use either an AutoOpen or AutoClose macro, depending on whether you want to update the fields when the document opens or closes. The following is an example of an AutoOpen macro you can use.

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

Note that the macro makes sure that the options are set to force updating the fields and links when printing occurs, then it updates all the members of the Fields collection in the document. If you, instead, wanted to update the fields at closing, you could use this macro:

Sub AutoClose()
    ActiveDocument.Fields.Update
End Sub

This macro is much shorter because there is no need to set the update-on-print options when you are exiting the document.exiting the document.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.