101

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
6
  • 1
    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, 2013 at 19:29
  • Just a heads up that I tried this in Word 2013, and confirming that it still works. Thank you very much @Gilles for providing the code!
    – Ugo
    May 26, 2015 at 9:48
  • What about a macro that goes to print preview and back to the document?
    – Pedro77
    Aug 7, 2015 at 12:57
  • @Pedro77 How is that supposed to help? At least with Word 2013 (I no longer have access to Word 2007), going to print preview, or for that matter printing, doesn't update fields. Aug 7, 2015 at 14:17
  • My fields are updated, at least references and cross-reference fields.
    – Pedro77
    Aug 7, 2015 at 18:08

7 Answers 7

37

Go into the print settings, select update fields. Then go to print, or print preview your doc.

Et voilà, all fields are updated!

MS Word Print Options from Word of Mac 2016

6
  • 2
    Working for me now in Word 2010 (where the setting is in“File → Options → Display”). In fact without the option some fields are updated but not all. I'm pretty sure it didn't in Word 2007 but I no longer have it to test. Sep 15, 2015 at 14:51
  • 2
    I'm on Word 2016 for Mac. The setting is in Word -> Preferences -> Print. But on widows it will in the print section of the global settings. I'm sure I've been there in the past, but I don't have an install to test with right now. Sep 15, 2015 at 15:55
  • I guess this doesn't work anymore on Word 2016.
    – TCB13
    Jan 7, 2016 at 16:34
  • This worked for me in Word 2016 on Windows 7.
    – bouvierr
    Sep 27, 2016 at 15:33
  • 1
    Pretending to print works with Word 2010. Not so intuitive…
    – M. Toya
    Dec 10, 2018 at 9:11
86

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
3
  • @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, 2010 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). Oct 7, 2010 at 19:02
  • This does not work for fields contained in text boxes contained in the header / footer. Checked on Word 2016
    – slobo
    Jun 8, 2019 at 19:00
5

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.

3

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.

2
  • How does it differ from pressing F9? Does that really update inside figures, tables, etc? Jul 18, 2013 at 19:44
  • 1
    I now have Word 2013, so I checked. This appears to do the same as pressing F9. It doesn't update fields inside figures, which was my main motivation for asking this question. Aug 7, 2015 at 14:13
3

If you want to properly update all headers and footers, this worked for me:

    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
1
2

For C#:

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main(string[] args)
    {
        List<string> path = new List<string>(args);
    
        string filePathstr = string.Join(" ", path.ToArray());
        //System.Windows.Forms.MessageBox.Show("filepathstr: " + filePathstr);

        string folderPathstr = Path.GetDirectoryName(filePathstr);
        //System.Windows.Forms.MessageBox.Show("folderPathstr: " + folderPathstr);

        try
        {
            Application ap = new Application();
            Document document = ap.Documents.Open(filePathstr);
            document.Fields.Update();

            foreach (Section section in document.Sections)
            {
                document.Fields.Update();  // update each section

                HeadersFooters headers = section.Headers;  //Get all headers
                foreach (HeaderFooter header in headers)
                {
                    Fields fields = header.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update();  // update all fields in headers
                    }
                }

                HeadersFooters footers = section.Footers;  //Get all footers
                foreach (HeaderFooter footer in footers)
                {
                    Fields fields = footer.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update();  //update all fields in footers
                    }
                }
            }    

            document.Save();
            document.Close();
        
        }
        catch (NullReferenceException)
        {
            System.Windows.Forms.MessageBox.Show("A valid file was not selected.");
        }
    }
}
1

The following have both worked for me:

Sub FieldsUpdateAlStoryMayor()
' Graham Mayor
' http://www.gmayor.com/installing_macro.htm
' update fields in a document
    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

Sub FieldsUpdateAllStoryMaxey()    ' Greg Maxey
' https://gregmaxey.com/word_tip_pages/word_fields.html
    Dim rngstory As Word.range
    Dim lngValidate As Long    ' do not know purpose of this - CK
    Dim oShp As Shape
    Dim oTOC As TableOfContents, oToa As TableOfAuthorities, oTof As TableOfFigures
    lngValidate = ActiveDocument.Sections(1).Headers(1).range.StoryType    ' do not know purpose of this
    For Each rngstory In ActiveDocument.StoryRanges
        'Iterate through all linked stories
        Do
            On Error Resume Next
            rngstory.Fields.Update
            Select Case rngstory.StoryType
                Case 6, 7, 8, 9, 10, 11
                    If rngstory.ShapeRange.Count > 0 Then
                        For Each oShp In rngstory.ShapeRange
                            If oShp.TextFrame.HasText Then
                                oShp.TextFrame.TextRange.Fields.Update
                            End If
                        Next
                    End If
                Case Else
                    'Do Nothing
            End Select
            On Error GoTo -1
            'Get next linked story (if any)
            Set rngstory = rngstory.NextStoryRange
        Loop Until rngstory Is Nothing
        'Special Cases
        For Each oTOC In ActiveDocument.TablesOfContents
            oTOC.Update
        Next oTOC
        For Each oToa In ActiveDocument.TablesOfAuthorities
            oToa.Update
        Next
        For Each oTof In ActiveDocument.TablesOfFigures
            oTof.Update
        Next
    Next
End Sub

These both address, in different ways, that fields can be in "stories" other than the main document story. You can go to their websites to see the theory/ideas behind the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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