http://answers.microsoft.com/en-us/office/forum/office_2007-office_other/how-do-i-hyperlink-specific-text-within-the-same/598cfd98-6217-4eac-9ac9-969477c46401?授权=1
“这在 Pub 2007 中相当简单。只需插入 > 书签并将该图标拖到您想要链接的位置。然后选择文本 > 插入超链接 > 放置在此文档中并选择您刚刚创建的书签。唯一一次我遇到的问题是如果页面在书签下方不够长......并且有解决方法
。http: //office.microsoft.com/en-us/publisher-help/create-a-hyperlink-HP010203490.aspx
大卫F"
让我知道这是否有帮助,或者您是否出于某种原因需要在 VBA 中执行此操作
编辑:编写一个宏来刷新页面链接是相当容易的,但是对象模型似乎不太支持到书签的链接,除非我忽略了一些东西。我的解决方案由两部分组成。
首先,应该刷新的链接由以“page”开头的显示文本识别(LIKE “page *”)。刷新宏只是识别这些链接并将其显示文本更改为页面 X。但是,这不适用于指向书签的链接,在对象模型中,书签的行为似乎类似于指向页面的链接,除了它们引用的 pageID 不存在. 我花了很长时间试图弄清楚这个不存在的超链接和书签之间可能存在什么关系,但无济于事。相反,我创建了一种解决方法,您可以在其中手动链接超链接和带有标签对象的书签(使用不存在的超链接页面 ID 的值为书签创建标签)。
页面正常链接的说明
创建指向页面的超链接。它的文本必须以“page”开头(否则必须编辑 RefreshReferenceLinks)
运行 C_RefreshReferenceLinks 以刷新以检查它是否有效
书签链接说明(标记解决方法)
创建书签(插入 -> 书签)
创建指向书签的超链接
选择超链接并运行 A_GetPageIdOfHyperlink
选择书签并运行 B_TagBookmarkWithPageId
运行 C_RefreshReferenceLinks 以刷新以检查它是否有效
您可以在此处下载包含示例内容、说明和宏的示例项目:http ://www.filedropper.com/showdownload.php/pageandbookmarklinks (它可能会给您一个安全警告,因为它包含宏)
完整来源
Public Const tagName = "BookmarkPageId"
Sub A_GetPageIdOfHyperlink()
Dim oHyperlink As Hyperlink
Set oHyperlink = ActiveDocument.Selection.TextRange.Hyperlinks(1)
CopyText oHyperlink.pageId
MsgBox oHyperlink.pageId & " copied to clipboard as text"
End Sub
Sub B_TagBookmarkWithPageId()
Dim oShape As Shape
Set oShape = ActiveDocument.Selection.ShapeRange(1)
If IsBookmark(oShape) Then
If TagExists(oShape.Tags, tagName) Then
oShape.Tags(tagName).Delete
End If
Dim txt As String
txt = Trim(GetClipBoardText())
Debug.Print "Ssdsd:" & txt
Dim newTag As Tag
Set newTag = oShape.Tags.Add(tagName, txt)
MsgBox "Tagged as " & tagName & " = '" & txt & "'"
Else
MsgBox "Not a bookmark"
End If
End Sub
Sub C_RefreshReferenceLinks()
Dim oPage As Page
Dim oShape As Shape
For Each oPage In ActiveDocument.Pages
For Each oShape In oPage.Shapes
RefreshInShape oShape
Next oShape
Next oPage
For Each oPage In ActiveDocument.MasterPages
For Each oShape In oPage.Shapes
RefreshInShape oShape
Next oShape
Next oPage
For Each oShape In ActiveDocument.ScratchArea.Shapes
RefreshInShape oShape
Next oShape
End Sub
Function RefreshInShape(oShape As Shape)
Dim cHyperlinks As Hyperlinks
Dim oHyperlink As Hyperlink
If oShape.HasTextFrame = False Then Exit Function
Set cHyperlinks = oShape.TextFrame.TextRange.Hyperlinks
For i = 1 To cHyperlinks.Count
Set oHyperlink = cHyperlinks(i)
If oHyperlink.TargetType = pbHlinkTargetTypePageID Then
If oHyperlink.TextToDisplay Like "page *" Then
oHyperlink.TextToDisplay = "page " & GetPageNumberByPageId(oHyperlink.pageId)
End If
End If
Next i
End Function
Function GetPageNumberByPageId(pageId)
Dim oPage As Page
Dim oShape As Shape
Dim oTag As Tag
For Each oPage In ActiveDocument.Pages
If CLng(oPage.pageId) = CLng(pageId) Then
GetPageNumberByPageId = oPage.PageNumber
Exit Function
End If
Next oPage
For Each oPage In ActiveDocument.Pages
For Each oShape In oPage.Shapes
If TagExists(oShape.Tags, tagName) Then
Set oTag = oShape.Tags(tagName)
If CStr(oTag.Value) = CStr(pageId) Then
GetPageNumberByPageId = oPage.PageNumber
Exit Function
End If
End If
Next oShape
Next oPage
GetPageNumberByPageId = "[ERROR]"
End Function
Function IsBookmark(oShape As Shape)
IsBookmark = False
If oShape.Type = pbWebHTMLFragment And oShape.AutoShapeType = msoShapeMixed Then
IsBookmark = True
End If
End Function
Function TagExists(collection As Tags, itemName As String) As Boolean
TagExists = False
Dim oTag As Tag
For Each oTag In collection
If oTag.Name = itemName Then
TagExists = True
Exit For
End If
Next oTag
End Function
Function GetParentOfType(obj As Object, sTypeName As String)
Do Until TypeName(GetParentOfType) = "Page"
Set GetParentOfType = obj.Parent
Loop
End Function
Sub CopyText(Text As String)
'VBA Macro using late binding to copy text to clipboard.
'By Justin Kay, 8/15/2014
'Thanks to http://akihitoyamashiro.com/en/VBA/LateBindingDataObject.htm
Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
MSForms_DataObject.SetText Text
MSForms_DataObject.PutInClipboard
Set MSForms_DataObject = Nothing
End Sub
Function GetClipBoardText() As String
Set DataObj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
On Error GoTo Whoa
'~~> Get data from the clipboard.
DataObj.GetFromClipboard
'~~> Get clipboard contents
GetClipBoardText = DataObj.GetText(1)
Exit Function
Whoa:
GetClipBoardText = ""
End Function