Consulting

Results 1 to 7 of 7

Thread: Table of Contents issue with Hyperlinks switch

  1. #1

    Table of Contents issue with Hyperlinks switch

    I have a table of contents that uses the hyperlinks switch "\h" so all entries in the TOC are hyperlinks. I'm trying to use VBA to combine some TOC entries into a single paragraph. VBA is not recognizing the tab character in the TOC entries when the hyperlinks switch is used. If I remove the hyperlinks switch, vba is able to recognize a tab character in the TOC. Below is the VBA code I'm trying to use to combine some entries into a single paragraph in the TOC. Note - I've removed portions of the code that aren't relevant to my issue. I added a comment to the line that isn't working as I expected. Is this a bug in Word? Is there another method I can use to recognize the tab character while using the hyperlinks switch in the TOC?

    Set rangeTOC = ActiveDocument.TablesOfContents(1).Range
    For lcount = (rangeTOC.Paragraphs.count - 1) To 1 Step -1
    Set myPara = rangeTOC.Paragraphs(lcount)
    Set myRange = myPara.Range.Characters.Last
    With myRange
    .MoveStartUntil Cset:=vbTab, count:=wdBackward 'this doesn't recoginize the tab character when the hyperlinks switch is used and extends the range to the beginning of the paragraph
    .MoveStart unit:=wdCharacter, count:=-1
    .Delete
    End With
    Next lcount

    Tab picture.jpg

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,483
    Location
    Trying to combine separate TOC entries is futile - anything that causes Word to refresh the TOC (e.g. printing the document) will revert it to its previous state.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  3. #3
    Hi Paul, what I'm building is a custom TOC refresh macro and the TOC will not get refreshed in any other manner (e.g., when printing) because I've locked the TOC field after the refresh macro is run.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,483
    Location
    You could delete the tabs with code like:
    Dim i As Long
    With ActiveDocument.TablesOfContents(1).Range
      For i = 1 To .Paragraphs.Count - 1
        With .Paragraphs(i).Range
          .End = .End - 1
          .Words.Last.Previous.Delete
        End With
      Next
    End With
    Cheers
    Paul Edstein
    [MS MVP - Word]

  5. #5
    That's part of the equation. The other part is that I need to delete the page number to the right of the tab which can be any number of characters long.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,483
    Location
    Perhaps you should reconsider the approach you're taking. For example, you might create a TOC without the page #s, then simply insert them via PAGEREF fields where required. Alternatively, you might use something based on the approach taken at: http://www.msofficeforums.com/word/3...tml#post113050
    Last edited by macropod; 06-29-2018 at 05:59 PM.
    Cheers
    Paul Edstein
    [MS MVP - Word]

  7. #7
    Thanks Paul. I ended up with an approach similar to your suggestion. I removed the hyperlink switch from the TOC code so the TOC generates without hyperlinks and then used a macro to add hyperlinks to each heading in the TOC. Here's the code I used (there may be a cleaner method, but this one seems to work well).

    Set rngTOC = ActiveDocument.TablesOfContents(1).Range
    'add hyperlink to first heading in TOC
    Set paraRange = rngTOC.Paragraphs(1).Range.Characters.Last
    paraRange.Collapse wdCollapseStart
    paraRange.MoveStartUntil vbTab, wdBackward
    For Each fld In paraRange.Fields
      If fld.Type = wdFieldPageRef Then
        strTOC = Trim(Mid(fld.Code.Text, 9))
        strTOC = Left(strTOC, Len(strTOC) - 3)
      End If
    Next
    Set rngItem = rngTOC.Words.First
    rngItem.Collapse wdCollapseEnd
    rngItem.Move unit:=wdCharacter, count:=-1
    rngItem.MoveEndUntil vbTab, wdForward
    rngItem.MoveEnd wdCharacter, 1
    rngItem.MoveEndUntil vbTab, wdForward
    rngItem.Hyperlinks.Add Anchor:=rngItem, SubAddress:=strTOC
    'add hyperlink to remaining headings in TOC
    For i = 2 To rngTOC.Paragraphs.count Step 1
      Set Para = rngTOC.Paragraphs(i)
      If Para.Range.Fields.count <> 1 Then
        GoTo nextparagraph
      End If
      For Each fld In Para.Range.Fields
        If fld.Type = wdFieldPageRef Then
          strTOC = Trim(Mid(fld.Code.Text, 9))
          strTOC = Left(strTOC, Len(strTOC) - 3)
        End If
      Next
      Set paraRange = rngTOC.Paragraphs(i).Range
      Set startRange = paraRange.Words.First
      startRange.Collapse wdCollapseStart
      Set endRange = rngTOC.Paragraphs(i).Range.Words.Last
      endRange.MoveUntil vbTab, wdBackward
      endRange.Move wdCharacter, -1
      rngItem.SetRange Start:=startRange.Start, End:=endRange.Start
      rngItem.Hyperlinks.Add Anchor:=rngItem, SubAddress:=strTOC
    nextparagraph:
    Next i

Posting Permissions

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