Last active
October 18, 2021 21:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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