Update all fields in MS Word 2003 / 2007 / 2010

To update all your fields in a MS Word document is a real pain in the @ss. The header and footer are not updated automatically, and if you use sections, you usually have to update them seperately as well. Or what about having cross references within your headings and a table of contents. Word first updates the table of contents, and only then the cross references in the header. This leaves the Table of Contents to be incomplete.

The macro below should always function. It may be a bit slow (I’m open to suggestions) but it least it will update any document, with any specific construction.


‘ Macro updates all fields within MS Word document. Save this macro in your normal.dot, and attach it to your menu!

‘ Short explanation: This macro truely updates the complete document, including subsections, header, footer, and even if cross links are used in headings.

Sub UpdateAllFields()
‘ All Story Field Updater
On Error GoTo ErrHandler
Dim oStory As Range
Dim TOCCount As Integer
‘ update footers
Dim oSection As Section
Dim oFooter As HeaderFooter
Dim cntField As Integer
Dim actWindow As Window
Dim startPage As Integer
   
    Set actWindow = ActiveDocument.ActiveWindow
    startPage = actWindow.Selection.Information(wdActiveEndPageNumber)
   
    Application.StatusBar = “Updating all fields within Document (1/7)”
    DoEvents
    ActiveDocument.Fields.Update
   
    Application.StatusBar = “Updating all fields within Stories (2/7)”
    DoEvents
   
    ‘
    ‘ Update all fields in all Story parts
    ‘
    For Each oStory In ActiveDocument.StoryRanges
      Do
        cntField = 0
        While cntField < oStory.Fields.Count
            cntField = cntField + 1
            oStory.Fields(cntField).Update
        Wend
       
        If (oStory.Fields.Count > 0) Then oStory.Fields.Update
        Set oStory = oStory.Next
        Loop Until oStory Is Nothing
    Next
    DoEvents
   
    With actWindow
      .Selection.WholeStory
      .Selection.Fields.Update
    End With
    DoEvents
   
    ‘
    ‘ Update all the Table of Contents within the document
    ‘
    Application.StatusBar = “Updating all Table of Contents (3/7)”
    DoEvents
    TOCCount = ActiveDocument.TablesOfContents.Count
    While TOCCount > 0
      ActiveDocument.TablesOfContents(TOCCount).Update
      TOCCount = TOCCount – 1
    Wend
    DoEvents
   
    Application.StatusBar = “Updating active panes (4/7)”
    DoEvents
   
    ‘
    ‘ Update the active panes: footers in the sections & all fields within the document, including the main index
    ‘
    With actWindow
      .ActivePane.View.SeekView = wdSeekMainDocument
      .Selection.HomeKey Unit:=wdStory
    End With
   
    Application.StatusBar = “Updating headers and footers (5/7)”
    DoEvents
   
    ‘
    ‘ Make sure that headers and footers become visible
    ‘
    If actWindow.View.SplitSpecial <> wdPaneNone Then
      actWindow.Panes(2).Close
    End If
    If actWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
      ActivePane.View.Type = wdOutlineView Then
      actWindow.ActivePane.View.Type = wdPrintView
    End If
    actWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
   
    Application.StatusBar = “Updating sections (6/7)”
    DoEvents
    For Each oSection In ActiveDocument.Sections
      For Each oFooter In oSection.Footers
        If oFooter.Exists Then
          cntField = 0
          While cntField < oFooter.Range.Fields.Count
            cntField = cntField + 1
            oFooter.Range.Fields(cntField).Update
          Wend
        End If
      Next oFooter
    Next oSection
   
    Application.StatusBar = “Updating the document (again) (7/7)”
    DoEvents
   
    ‘
    ‘ update the document again, to ensure that if there are cross links within the headers, they are updated as well.
    ‘
    With actWindow
      .ActivePane.View.SeekView = wdSeekMainDocument
      .Selection.WholeStory
      .Selection.Fields.Update
    End With
   
    actWindow.Selection.GoTo What:=wdGoToPage, Count:=startPage
    Call MsgBox(“Update of all fields is completed.”)
    GoTo Einde
ErrHandler:
    MsgBox “Error in UpdateAllFieldsn” + Err.Description + “(” + Str(Err.Number) + “)”, vbMsgBoxHelpButton, “Error in UpdateAllFields”, Err.HelpFile, Err.HelpContext
Einde:
End Sub