Skip to content

Instantly share code, notes, and snippets.

@tdalon
Last active October 18, 2021 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tdalon/2a1682d67c8190240aa54bb2993b85dc to your computer and use it in GitHub Desktop.
Save tdalon/2a1682d67c8190240aa54bb2993b85dc to your computer and use it in GitHub Desktop.
MS Office Word VBA Macro to update all fields including in header and footer also update Table of Contents. Tracking Changes is disabled during the update.
Sub UpdateAllFieldsTOCWithoutTracking()
' Update All Fields and TOC without Tracking Changes for it:
' References:
' http://www.gmayor.com/installing_macro.htm or
' http://stackoverflow.com/questions/33733113/macro-to-update-all-fields-in-a-word-document
' http://gregmaxey.mvps.org/word_tip_pages/word_fields.html
' http://vba.relief.jp/word-vba-toggle-track-change/
Dim oStory As Range
Dim isChangesTracked As Boolean
' Toggle off Track Changes
isChangesTracked = ActiveDocument.TrackRevisions
If isChangesTracked Then
ActiveDocument.TrackRevisions = False
End If
' Update Fields
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
' Update TableOfContents
If ActiveDocument.TablesOfContents.Count = 1 Then
ActiveDocument.TablesOfContents(1).Update
End If
' Restore Changes Tracking
If isChangesTracked Then
ActiveDocument.TrackRevisions = True
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment