在 Microsoft Publisher 2010 中插入对页码的引用

Insert reference to page number in Microsoft Publisher 2010

我是 MS Publisher 2010 的新手,我正在尝试添加对特定页面的 "dynamic" 引用。理想情况下,可视化文本应该是这样的:

...see the example on page XXX

我想让 XXX 部分可视化我所指页面的页码。我看到您可以在文档中放置书签,并创建指向这些书签的超链接,但到目前为止我无法想象与书签相关联的页码。

再举一个例子,我想要这个 Latex 表达式的等价物:

...see the example on page~\pageref{reference-to-XXX}

是否可以在 Publisher 2010 中获得这种效果,也许使用 VB 脚本?感谢您的帮助!

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?auth=1

"This is fairly easy with Pub 2007. Just Insert > bookmark and drag that icon to where you want the link to go. Then select the text >insert hyperlink > place in this document and choose the bookmark that you just created. The only time I have had problems is if the page is not long enough below the bookmark...and there are workarounds. http://office.microsoft.com/en-us/publisher-help/create-a-hyperlink-HP010203490.aspx DavidF"

让我知道这是否有帮助,或者您是否出于某种原因需要在 VBA

中这样做

编辑: 编写一个宏来刷新页面的 links 是相当容易的,但是对象模型似乎对书签的 links 的支持很差,除非我忽略了一些东西。我的解决方案由两部分组成。

首先,应该刷新的 links 由它们以 "page " 开头的显示文本识别(LIKE "page *")。刷新宏只是识别那些 links 并将它们的显示文本更改为第 X 页。但是,这不适用于 links 到书签,这在对象模型中似乎表现得像 links 到页面,除了它们引用的 pageID 不存在。我花了很长时间试图弄清楚这个不存在的 hyperlink 和书签之间可能有什么关系,但无济于事。相反,我创建了一个解决方法,您可以在其中手动 link hyperlink 和带有标签对象的书签(使用 hyper 的不存在的页面 ID 的值为书签创建标签link).

正常 link 页的说明

  1. 为页面创建 hyperlink。它的文本必须以“page”开头 (否则必须编辑 RefreshReferenceLinks)

  2. 运行 C_RefreshReferenceLinks 刷新以检查它是否有效

link 书签说明(标记解决方法)

  1. 创建书签(插入 -> 书签)

  2. 为书签创建一个超级link

  3. Select hyperlink 和 运行 A_GetPageIdOfHyperlink

  4. Select 书签和 运行 B_TagBookmarkWithPageId

  5. 运行 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