Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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