How to dynamically hyperlink a Table of Contents to pages in Publisher vba?

116 Views Asked by At

I have a Publisher booklet with a table of contents on page 2. The page is based on a master page, but the table itself is not on the master page. The TOC is in a table. I would like the end result to read the contents of each cell in the TOC, compare the text to the page titles (are on actual pages; not master pages). If a match is found, create/refresh the hyperlink and display the corresponding page number.

When I get to the hyperlink portion of the code, I get an error at Text:= .TextRange (compile error - method or data not found). I have tried swapping out a few different segments but was then getting Error 70 Permission Denied. I have also included a snippet of what the TOC looks like, in case that helps. TOC image

Any guidance that can be provided is greatly appreciated. Thank you!!!

I have listed my code below.

Sub HyperlinkTOC()

Dim hypNew As Hyperlink
Dim lngPageID As Long
Dim strPage As String
Dim intCount As Integer
    x = ActiveDocument.Pages.Count
    lngPageID = ActiveDocument.Pages(x).PageID
    strPage = "Go to page " _
    & Str(ActiveDocument.Pages(x).PageNumber)

 With Selection

    If .Type = pbSelectionTableCells Then

        With .TableCellRange

            For intCount = 1 To .Count

            .Item (intCount)

                If Selection.TextRange.Hyperlinks.Count >= 1 Then
                Selection.TextRange.Hyperlinks(1).Delete
                End If

 '>>>Error starts here

                Set hypNew = .TextRange.Hyperlinks.Add(Text:=.TextRange, _
                RelativePage:=pbHlinkTargetTypePageID, _
                PageID:=lngPageID, _
                TextToDisplay:=strPage)

                End With

            Next

        End With

    End If

End With

End Sub
0

There are 0 best solutions below