title: Macro to update all fields in a word document
body: <p>I have built - over the years - a vba macro that is supposed to update all fields in a word document. </p>
  
  <p>I invoke this macro before releasing the document for review to ensure all headers and footers etc are correct.</p>
  
  <p>Currently - it look like this:</p>
  
  <pre><code>Sub UpdateAllFields()
  '
  ' UpdateAllFields Macro
  '
  '
      Dim doc As Document ' Pointer to Active Document
      Dim wnd As Window ' Pointer to Document's Window
      Dim lngMain As Long ' Main Pane Type Holder
      Dim lngSplit As Long ' Split Type Holder
      Dim lngActPane As Long ' ActivePane Number
      Dim rngStory As Range ' Range Objwct for Looping through Stories
      Dim TOC As TableOfContents ' Table of Contents Object
      Dim TOA As TableOfAuthorities 'Table of Authorities Object
      Dim TOF As TableOfFigures 'Table of Figures Object
      Dim shp As Shape
  
      ' Set Objects
      Set doc = ActiveDocument
      Set wnd = doc.ActiveWindow
  
      ' get Active Pane Number
      lngActPane = wnd.ActivePane.Index
  
      ' Hold View Type of Main pane
      lngMain = wnd.Panes(1).View.Type
  
      ' Hold SplitSpecial
      lngSplit = wnd.View.SplitSpecial
  
      ' Get Rid of any split
      wnd.View.SplitSpecial = wdPaneNone
  
      ' Set View to Normal
      wnd.View.Type = wdNormalView
  
      ' Loop through each story in doc to update
      For Each rngStory In doc.StoryRanges
          If rngStory.StoryType = wdCommentsStory Then
              Application.DisplayAlerts = wdAlertsNone
              ' Update fields
              rngStory.Fields.Update
              Application.DisplayAlerts = wdAlertsAll
          Else
             ' Update fields
             rngStory.Fields.Update
              If rngStory.StoryType &lt;&gt; wdMainTextStory Then
                  While Not (rngStory.NextStoryRange Is Nothing)
                      Set rngStory = rngStory.NextStoryRange
                      rngStory.Fields.Update
                  Wend
              End If
          End If
      Next
  
      For Each shp In doc.Shapes
        If shp.Type &lt;&gt; msoPicture Then
          With shp.TextFrame
              If .HasText Then
                  shp.TextFrame.TextRange.Fields.Update
              End If
          End With
        End If
      Next
  
      ' Loop through TOC and update
      For Each TOC In doc.TablesOfContents
          TOC.Update
      Next
  
      ' Loop through TOA and update
      For Each TOA In doc.TablesOfAuthorities
          TOA.Update
      Next
  
      ' Loop through TOF and update
      For Each TOF In doc.TablesOfFigures
          TOF.Update
      Next
  
      ' Header and footer too.
      UpdateHeader
      UpdateFooter
  
      ' Return Split to original state
      wnd.View.SplitSpecial = lngSplit
  
      ' Return main pane to original state
      wnd.Panes(1).View.Type = lngMain
  
      ' Active proper pane
      wnd.Panes(lngActPane).Activate
  
      ' Close and release all pointers
      Set wnd = Nothing
      Set doc = Nothing
  
  End Sub
  
  Sub UpdateFooter()
      Dim i As Integer
  
       'exit if no document is open
      If Documents.Count = 0 Then Exit Sub
      Application.ScreenUpdating = False
  
       'Get page count
      i = ActiveDocument.BuiltInDocumentProperties(14)
  
      If i &gt;= 1 Then 'Update fields in Footer
          For Each footer In ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers()
           footer.Range.Fields.Update
          Next
      End If
  
      Application.ScreenUpdating = True
  End Sub
  
   'Update only the fields in your footer like:
  Sub UpdateHeader()
      Dim i As Integer
  
       'exit if no document is open
      If Documents.Count = 0 Then Exit Sub
      Application.ScreenUpdating = False
  
       'Get page count
      i = ActiveDocument.BuiltInDocumentProperties(14)
  
      If i &gt;= 1 Then 'Update fields in Header
          For Each header In ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers()
           header.Range.Fields.Update
          Next
      End If
  
      Application.ScreenUpdating = True
  End Sub
  </code></pre>
  
  <p>I have noticed recently that it sometimes misses some secions of the document. Today it missed <em>First page footer -section 2-</em> because the document version was not updated.</p>
  
  <p>I have built this macro over a number of years and several bouts of research but I am not proud of it so please suggest a complete replacement if there is now a clean way of doing it. I am using Word 2007.</p>
  
  <p>To test, create a word document and add a custom field named <code>Version</code> and give it a value. Then use that field <code>{DOCPROPERTY Version \* MERGEFORMAT }</code> in as many places as you can. Headers, Footers, first-page, subsequent page etc. etc. Remember to make a multi-section document with different header/footers. Then change the property and invoke the macro. It currently does quite a good job, handling TOCs and TOAs an TOFs etc, it just seems to skip footers (sometimes) in a multi-section document for example.</p>
  
  <h2>Edit</h2>
  
  <p>The challenging document that seems to cause the most problems is structured like this:</p>
  
  <p>It has 3 sections. </p>
  
  <p>Section 1 is for the title page and TOC so the first page of that section has no header/footer but does use the <code>Version</code> property on it. Subsequent pages have page numbering in roman numerals for the TOC.</p>
  
  <p>Section 2 is for the body of the document and has headers and footers.</p>
  
  <p>Section 3 is for the copyright blurb and this has a very strange header and a cut-down footer.</p>
  
  <p>All footers contain the <code>Version</code> custom document property.</p>
  
  <p>My code above seems to work in all cases except sometimes it misses first page footer of sections 2 and 3.</p>