• Word 97 – Problem with Updating TOC using VBA

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Word 97 – Problem with Updating TOC using VBA

    Author
    Topic
    #351705

    Hello,

    I have a macro that should automatically update the table of contents using VBA, the only problem is that it only appears to work the first time.
    I have a section of text at the beginning of a document that needs to be hidden before the doc can be printed. When you click a button to run the VBA and hide the text, the TOC is updated (should be) to make sure that everything matches up. After the document is printed, the button should be clicked again to show the text for online viewing. The TOC should be updated again, but the function only appears to update the TOC the first time. Here is what I have currently for updating the TOC:

    Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:=”TOC”
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = “”
    .Replacement.Text = “”
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Fields.Update

    I have tried many other things as well, so I won’t list them here, but if anyone has seen this happen or can help me in any way, I would greatly appreciate it!

    Thanks!

    Viewing 2 reply threads
    Author
    Replies
    • #509843

      A more efficient way to get to the TOC and update it is to set a reference to it, as in the code below:

      Sub UpdateTOC()
      Dim rngTOCRng As Range

      Set rngTOCRng = ActiveDocument.TablesOfContents(1).Range
      rngTOCRng.Fields.Update

      End Sub

      You should be able to repeat the “rngTOCRng.Fields.Update” statement as many times as you need.

      The code you’ve got now was gotten by recording, but it’s not the most efficient way to get to the TOC.

      Hope this helps.

    • #509849

      I think the problem is not the TOC but the hidden text. If your “Click” to hide text is just revealing or not revealing the hidden text (which can be printed or not printed) then the TOC thing is working fine. TOC’s do not include text with the hidden attribute so it would not appear in the TOC no matter how many times you update it.

      Gary’s code is cleaner than the stuff you recorded but you could also delete all except the first and last lines to speed up your own code.

    • #509880

      Hi Gary and Andrew,

      Thank you for your quick responses! I tried your suggestions, but they still didn’t work. The TOC field would only update the first time the code was run (button clicked). I changed it to use the UpdatePageNumbers (just to try!) and now it is working (?).

      Thanks again!

    Viewing 2 reply threads
    Reply To: Word 97 – Problem with Updating TOC using VBA

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: