Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    NJ, USA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word 97 - Problem with Updating TOC using VBA

    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!

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 28 Times in 27 Posts

    Re: Word 97 - Problem with Updating TOC using VBA

    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.

  4. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,499
    Thanks
    3
    Thanked 139 Times in 132 Posts

    Re: Word 97 - Problem with Updating TOC using VBA

    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #4
    Lounger
    Join Date
    Jan 2001
    Location
    NJ, USA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word 97 - Problem with Updating TOC using VBA

    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!

Posting Permissions

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