Consulting

Results 1 to 7 of 7

Thread: Lock table of contents using a macro

  1. #1

    Lock table of contents using a macro

    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?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    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

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    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.
    Last edited by Knightfall; 12-18-2018 at 11:01 AM.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Knightfall View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    Quote Originally Posted by gmaxey View Post
    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.

    Quote Originally Posted by macropod View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •