我有一个出版商小册子,第 2 页上有目录。该页面基于母版页,但表本身不在母版页上。TOC 在表格中。我希望最终结果能够读取目录中每个单元格的内容,将文本与页面标题进行比较(在实际页面上;而不是母版页)。如果找到匹配项,则创建/刷新超链接并显示相应的页码。
当我到达代码的超链接部分时,在 Text:= .TextRange 处出现错误(编译错误 - 找不到方法或数据)。我尝试换出几个不同的段,但随后出现错误 70 权限被拒绝。我还包括了 TOC 的一个片段,以防万一。目录图像
非常感谢可以提供的任何指导。谢谢!!!
我在下面列出了我的代码。
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