PDA

View Full Version : [SOLVED:] Table of Contents issue with Hyperlinks switch



tazzmonster
06-28-2018, 10:45 AM
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

22492

macropod
06-28-2018, 02:14 PM
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.

tazzmonster
06-28-2018, 02:31 PM
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.

macropod
06-28-2018, 03:06 PM
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

tazzmonster
06-29-2018, 08:00 AM
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.

macropod
06-29-2018, 05:47 PM
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/34947-clickable-toc-links-word-pdf.html#post113050

tazzmonster
07-03-2018, 12:58 PM
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