• Formatting TOC Page numbers (VBA Word 2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Formatting TOC Page numbers (VBA Word 2000)

    Author
    Topic
    #372733

    This only happens if I use the h option in the TOC field. I need to be able to add a character style to the page numbers in a TOC. WithOUT the h in the TOC field, I can do VBA to select just the page number. However, I can find no way to do this WITH the h option.

    Thanks for your help!!
    Troy

    Viewing 1 reply thread
    Author
    Replies
    • #597515

      Since noone else seemed to have an answer, I found a clunky but functional workaround I thought I would share.

      The problem I was having was selecting just the page numbers at the end of the paragraphs in the TOC as well as some chapter numbers that appeared at the beginning of TOC 1 paragraphs. The problem with doing this with the hyperlink option (h) enabled is that you cannot select the first or last character of a paragraph. Without that option you can select whatever you want.

      My workaround:

      1. Update the table of contents.

      2. Temporarily unlink the field by selecting the entire paragraph and running the command:

      Selection.Fields.Unlink

      3. Copy the first or last character as needed.

      4. ActiveDocument.Undo 1 (which reverts back to the hyperlink functionality)

      5. Paste the character either just after the first character or just before the last character depending on what I copied.

      6. Now I can select all but the first or last character and apply whatever formatting I want.

      I also found I could use a Find, looking for “^t^#” (without wildcards ONLY). This finds the tab before the page number and the entire page number regardless of how many digits the page number is. Perhaps this little advantage is a result of the way the hyperlink or fields are constructed. NOTE: I could not do a replace to add formatting as you would think, but I could apply formatting, such as a style directly after the find. Thus, I could use if/then or while selection.find.found to apply the formatting as needed.

      If you can improve on this, please let me know. It works, but I know it’s not the prettiest solution in the world.

      Thanks!!
      Troy

    • #597520

      The lack of reply wasn’t due to lack of interest, but because I have had the same problem and not been able to solve it either.

      StuartR

      • #597524

        I did not mean to imply there had been no interest. I just didn’t see any answers. I assume when that happens I’ve just put out a rare stumper. If you happen to find any better way, please let me know. Otherwise, enjoy the solution!!

        Troy

    Viewing 1 reply thread
    Reply To: Reply #597524 in Formatting TOC Page numbers (VBA Word 2000)

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

    Your information:




    Cancel