View Single Post
 
Old 03-28-2017, 05:10 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Word for Mac uses OS X's built-in PDF creator, which doesn't do what you want. Word for Windows uses either Adobe's PDF creator or its own (depending on the Word version), and either of those will create a document with working Table of Contents links.

The following macro converts a Table of Contents to Hyperlinks (with or without page #s). IIRC, 'proper' hyperlinks survive the Mac conversion process. Since hyperlinks don't update the way TOC fields do, it's probably best to not do the conversion until the document is otherwise finalised.
Code:
Sub ConvertTOC2Hyperlinks()
Dim RngTOC As Range, RngItem As Range, StrBkMkList As String, StrTmp As String, i As Long
With ActiveDocument
  With .TablesOfContents(1)
    .Update
    If InStr(.Range.Fields(1).Code.Text, "\n") = 0 Then
      For i = 3 To .Range.Fields.Count Step 2
        StrBkMkList = StrBkMkList & "|" & Split(Trim(.Range.Fields(i).Code.Text), " ")(1)
      Next
    Else
      For i = 2 To .Range.Fields.Count
        StrBkMkList = StrBkMkList & "|" & Replace(Split(Trim(.Range.Fields(i).Code.Text), " ")(2), """", "")
      Next
    End If
    Set RngTOC = .Range
  End With
  RngTOC.Fields.Unlink
  For i = 1 To UBound(Split(StrBkMkList, "|"))
    Set RngItem = RngTOC.Paragraphs(i).Range
    With RngItem
      .MoveEndUntil vbTab, wdBackward
      .End = .End - 1
    End With
    StrTmp = Replace(Split(StrBkMkList, "|")(i), "Toc", "HL")
    .Bookmarks.Add Name:=StrTmp, Range:=.Bookmarks(Split(StrBkMkList, "|")(i)).Range
    .Bookmarks(Split(StrBkMkList, "|")(i)).Delete
    .Hyperlinks.Add Anchor:=RngItem, SubAddress:=StrTmp, TextToDisplay:=.Bookmarks(StrTmp).Range.Text
    ' Delete the next 2 lines to omit page #s
    Set RngItem = RngTOC.Paragraphs(i).Range.Words.Last.Previous.Words.First
    .Hyperlinks.Add Anchor:=RngItem, SubAddress:=StrTmp, TextToDisplay:=RngItem.Words.Last
    RngItem.Paragraphs.First.Range.Font.Reset
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote