PDA

View Full Version : Lock table of contents using a macro



Knightfall
12-18-2018, 09:28 AM
I have a table of contents being generated by a macro. Once this table of contents is made, I want to lock it so that no updates will be done to it. How can I do this in the same macro?

I tried to record a macro in Word, and I discovered when you highlight the table of contents and use the lock command (Ctrl+F11) the corresponding code is Selection.Fields.Locked = True

It seems this only works if I select the table of contents with my mouse (hence selection) and then use the command. I'd like to have this done automatically by the macro. Additionally, the table of contents can vary in length, so that would need to be taken into consideration as well.

Can this be done, and if so, how?

gmaxey
12-18-2018, 09:52 AM
The content of that macro is? Unless it is a state secret, it helps if you post your code.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 12/18/2018
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldTOC Then oFld.Locked = True
'If oFld.Type = wdFieldTOC Then oFld.Unlink 'If you want to make your TOC fixed text.
Next oFld
lbl_Exit:
Exit Sub
End Sub

Knightfall
12-18-2018, 10:00 AM
Its not a state secret. Below is the existing code.


Sub ToC()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "INSERT TOC HERE"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=False, UpperHeadingLevel:=1, _
LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
True
.TablesOfContents(1).TabLeader = wdTabLeaderDots
End With
Selection.Fields.Locked = True
End Sub

gmaxey
12-18-2018, 10:24 AM
Dim oTOC As TableOfContents
Selection.Find.ClearFormatting
With Selection.Find
.Text = "INSERT TOC HERE"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With ActiveDocument
Set oTOC = .TablesOfContents.Add(Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=False, UpperHeadingLevel:=1, _
LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
True)
End With
oTOC.TabLeader = wdTabLeaderDots
oTOC.Range.Fields(1).Locked = True
End Sub

Knightfall
12-18-2018, 10:40 AM
When I go to print the final document, some of the page numbers are still displayed as "Error, bookmark not defined!"

EDIT: This is working when I test it stand alone. When I test it as part of my larger program it does not work. I'll do some more investigating.

macropod
12-18-2018, 03:34 PM
When I go to print the final document, some of the page numbers are still displayed as "Error, bookmark not defined!"
Instead of compromising the functionality of your Table of Contents by locking it, you should fix the problem at its source.

Knightfall
12-18-2018, 05:45 PM
Dim oTOC As TableOfContents
Selection.Find.ClearFormatting
With Selection.Find
.Text = "INSERT TOC HERE"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With ActiveDocument
Set oTOC = .TablesOfContents.Add(Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=False, UpperHeadingLevel:=1, _
LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
True)
End With
oTOC.TabLeader = wdTabLeaderDots
oTOC.Range.Fields(1).Locked = True
End Sub


This worked perfectly. Thank you very much.


Instead of compromising the functionality of your Table of Contents by locking it, you should fix the problem at its source.

I agree, and after working with you earlier this year, that was the same conclusion I reached and voiced. Sadly, it was voted down.