102

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
phuclv
  • 30,396
  • 15
  • 136
  • 260

7 Answers7

87

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
phuclv
  • 30,396
  • 15
  • 136
  • 260
DMA57361
  • 18,793
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

R3uK
  • 402
David Roussel
  • 626
  • 6
  • 5
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.

phuclv
  • 30,396
  • 15
  • 136
  • 260
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.

Excellll
  • 12,847
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

phuclv
  • 30,396
  • 15
  • 136
  • 260
Yohnny
  • 31
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(&quot; &quot;, path.ToArray());
    //System.Windows.Forms.MessageBox.Show(&quot;filepathstr: &quot; + filePathstr);

    string folderPathstr = Path.GetDirectoryName(filePathstr);
    //System.Windows.Forms.MessageBox.Show(&quot;folderPathstr: &quot; + 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(&quot;A valid file was not selected.&quot;);
    }
}

}

phuclv
  • 30,396
  • 15
  • 136
  • 260
Sherd
  • 121
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.

phuclv
  • 30,396
  • 15
  • 136
  • 260