Macro – Update All Fields in Word Document

In this article, you will find a VBA macro that lets you update all fields in a Word document no matter where the fields are found. The macro will find and update fields not only in the main body but also in headers, footers, footnotes, endnotes, text boxes and other shapes, etc. The macro will even find and update fields in shapes inside drawing canvases anywhere in the document.

You can install and use the macro without any knowledge about macros and VBA.

Macro updated 5-Aug-2020 (by mistake, some code lines were missing in the previous version).

What you should know about the update fields macro

The macro below will update all fields in the active Word document no matter where the fields are found. By using a macro to update fields, you simply need to run the macro to have all fields updated. Alternatively, you might have to perform a number of manual actions to make sure that all fields are updated if they are spread around in different parts of Word such as the main story, headers, footers, footnotes, endnotes, text boxes and other shapes.

Note that the macro includes a number of comments that explain a little about what is going on. The information after the macro code below will cover it in more detail.

You can copy the macro code below and insert it either in your Normal.dotm file or in another Word file of your choice. For help on installing a macro, see How to Install a Macro.

Warnings that may be shown if you manually update fields in Word are automatically suppressed when using the Update Fields macro

If you attempt to manually update fields in comments, footnotes and endnotes, Word may show a warning as illustrated in Figure 1 below, telling that the action cannot be undone. To allow the update fields macro to update all fields anywhere in the document without being stopped by such alerts, the macro code turns off such alerts while running.

Warning – update fields – can't undo action

Figure 1. Warning that may be shown in Word if you attempt to update fields in a comment, footnote or endnote. The update fields macro below suppresses such warnings.

Locked fields will not be updated by the macro

You can lock fields in Word. The purpose of locking fields is normally that you want to prevent the fields from being updated.

The macro below does not update locked fields. All locked fields will remain unchanged. It is, however, possible to create a macro that checks each individual field, unlocks it if locked, updates the field and locks it again.

For information about how to manually lock and unlock fields, see How to prevent fields from being updated – locked fields.

Protected documents must be unprotected before using the macro

If a document is protected, the purpose is normally to prevent any changes or certain changes to be made to the document. In case of a protected document, the macro shows a message (see Figure 2 below), telling you to unprotect the document before running the macro. Thereby, it is up to you to decide whether it is OK to update fields in the document.

The message that will be shown if you attempt to update all fields in a protected document

Figure 2. This message will be shown if you attempt to update all fields in a protected document.

Track changes and updating fields – the Update Fields macro lets you turn it off during the update

If you manually update fields in a Word document while track changes is turned on, every updated field will be marked as revisions where the old field is deleted and a new one added. This is most often just disturbing noise in the document. The macro below checks whether track changes is on. If that is the case, the macro shows the message in Figure 3, letting you turn it off during the field update. If you do so, track changes will be turned on again before the macro finishes.

Illustration of message that will be shown if track changes is turned on in the document in which you attempt to update all fields

Figure 3. This message will be shown if track changes is turned on in the document in which you attempt to update all fields.

It is possible to extend the macro with code that turns off any protection, updates the fields and finally sets the protection back to the original.

The code in the macro that updates fields in text boxes, shapes, etc. is found twice. That code could be moved to a function that is called from the main macro. That way, it would be needed only once. However, I decided to allow the redundant code so that only one sub is needed, making it simpler for users who are new to VBA.

The macro – UpdateAllFieldsInDocument

Sub UpdateAllFieldsInDocument()
    
'=========================
'Macro created 2019 by Lene Fredborg, DocTools - www.thedoctools.com
'Revised August 2020 by Lene Fredborg
'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP THE LINE ABOVE.
'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.
'=========================
'The macro updates all fields in the activedocument no matter where the fields are found
'Includes fields in headers, footers, footnotes, endnotes, shapes, etc.
'=========================
    
    Dim oDoc As Document
    Dim rngStory As Range
    Dim oShape As Shape
    Dim oShape_2 As Shape
    Dim oTOC As TableOfContents
    Dim oTOF As TableOfFigures
    Dim oTOA As TableOfAuthorities
    Dim blnTurnOnTrackRevisions As Boolean

    On Error GoTo ErrorHandler
    
    If Documents.Count = 0 Then
        MsgBox "No documents are open.", vbOKOnly, "Update All Fields"
        Exit Sub
    Else
        Set oDoc = ActiveDocument
    End If

    With oDoc
        'Stop if document protection prevents full update of fields
        If .ProtectionType <> wdNoProtection Then
            MsgBox "The document is protected. In order to update all fields, you must first unprotect the document.", _
                vbOKOnly + vbInformation, "Update All Fields – Protected Document"
            GoTo ExitHere
        End If
        
        blnTurnOnTrackRevisions = False
        
        'Show msg if track changes is on
        'Let user turn it off to prevent all updated fields marked as revisions
        If .TrackRevisions = True Then
            If vbYes = MsgBox("Track changes is currently ON. Do you want to turn OFF track changes while updating fields?", _
                              vbYesNo + vbQuestion, "Turn Off Track Changes?") Then
                blnTurnOnTrackRevisions = True
                .TrackRevisions = False
            End If
        End If
    End With
    
    'Turn off screen updating for better performance
    Application.ScreenUpdating = False

    'Prevent alert when updating footnotes/endnotes/comments story
    Application.DisplayAlerts = wdAlertsNone

    'Iterate through all stories and update fields
    For Each rngStory In ActiveDocument.StoryRanges
        If Not rngStory Is Nothing Then
            'Update fields directly in story
            rngStory.Fields.Update
            
            If rngStory.StoryType <> wdMainTextStory Then
                'Update fields in shapes and drawing canvases with shapes
                For Each oShape In rngStory.ShapeRange
                    With oShape.TextFrame
                        If .HasText Then
                            .TextRange.Fields.Update
                        End If
                        
                        'In case of a drawing canvas
                        'May contain other shapes that may contain fields
                        If oShape.Type = msoCanvas Then
                            For Each oShape_2 In oShape.CanvasItems
                                With oShape_2.TextFrame
                                    If .HasText Then
                                        .TextRange.Fields.Update
                                    End If
                                End With
                            Next oShape_2
                        End If
                        
                    End With
                Next oShape
            End If

            'Handle e.g. multiple sections with unlinked headers/footers or linked text boxes
            If rngStory.StoryType <> wdMainTextStory Then
                While Not (rngStory.NextStoryRange Is Nothing)
                    Set rngStory = rngStory.NextStoryRange
                    rngStory.Fields.Update
                
                    'Update fields in shapes and drawing canvases with shapes
                    For Each oShape In rngStory.ShapeRange
                        With oShape.TextFrame
                            If .HasText Then
                                .TextRange.Fields.Update
                            End If
                            
                            'In case of a drawing canvas
                            'May contain other shapes that may contain fields
                            If oShape.Type = msoCanvas Then
                                For Each oShape_2 In oShape.CanvasItems
                                    With oShape_2.TextFrame
                                        If .HasText Then
                                            .TextRange.Fields.Update
                                        End If
                                    End With
                                Next oShape_2
                            End If
                            
                        End With
                    Next oShape
                Wend
            End If
        End If
    Next rngStory
    '=========================

    'Update any TOC, TOF, TOA
    For Each oTOC In oDoc.TablesOfContents
        oTOC.Update
    Next oTOC
    For Each oTOF In oDoc.TablesOfFigures
        oTOF.Update
    Next oTOF
    For Each oTOA In oDoc.TablesOfAuthorities
        oTOA.Update
    Next oTOA
    '=========================

ExitHere:
    On Error Resume Next
    'Restore to original track revisions if relevant
    If blnTurnOnTrackRevisions = True Then
        oDoc.TrackRevisions = True
    End If
    'Clean up
    Set oDoc = Nothing
    Set rngStory = Nothing
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = wdAlertsAll

    Exit Sub
    '=========================
ErrorHandler:
    'Make sure to display alerts again in case of an error
    Resume ExitHere
End Sub

Related information

You will find a number of other articles on this website related to macros and fields.

For help on installing a macro, see How to Install a Macro.

For detailed information about how Word updates fields, see my article Updating Fields in Word – How it Works.

For an overview of keyboards shortcuts related to fields, see my article about useful shortcuts related to fields.

For information about DocProperty fields, see my article How properties and DocProperty fields work on my website wordaddins.com

For details about cross-reference fields, see my article How cross-reference fields in Word work on my website wordaddins.com.

In case of problems with cross-reference fields not being updated as expected, see my article Cross-reference Problems - Troubleshooting.