【问题标题】:Hyperlinked text in Word footers to a selected bookmarkWord 页脚中的超链接文本到选定的书签
【发布时间】:2014-07-07 00:52:49
【问题描述】:

我希望在我的所有文档中都有一个自定义页脚,其中包含指向同一文档中书签的超链接文本。即所有页脚中的“文档顶部”类型的链接。我必须收集各地的信息才能实现这一目标。并想在这里分享,这样其他人就不必一次次为这件事而战。

到目前为止,从stackoverflow和其他网站的所有问题和建议来看,我已经取得了这么多-

  • 创建了一个宏来自动为文档中的选定文本创建书签。
  • 如果书签已经存在,将重新创建(删除并创建)
  • 宏将添加一个带有页码的新页脚和一个带有分隔符的文本(即/Hit Overview)。

现在我想在页脚中创建此文本的超链接到书签。代码很简单。但我想我做错了什么,尝试创建一个 HyperLink 对象。但不工作。请提出一些建议。

这里是宏函数-

        Sub InsertFootnote()
        Const wdAlignPageNumberCenter = 1
        Dim varNumberPages As Variant
        varNumberPages = ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)

        ' Delete bookmark if any with this name
        If ActiveDocument.Bookmarks.Exists("HitOverviewMac") = True Then
            ActiveDocument.Bookmarks("HitOverviewMac").Delete
        End If

        ' Create a Bookmark to the selected text
        With ActiveDocument.Bookmarks
            .Add Range:=Selection.Range, Name:="HitOverviewMac"
            .DefaultSorting = wdSortByName
            .ShowHidden = False
        End With
        Dim mHlink As Hyperlink
        Dim i As Long
        For i = 1 To ActiveDocument.Sections.Count
            With ActiveDocument.Sections(i)
                ' Remove footer
                '.Footers(wdHeaderFooterPrimary).Range.Text = ""
                '.Footers(wdHeaderFooterPrimary).PageNumbers.Add (wdAlignPageNumberCenter)
                '.Footers(wdHeaderFooterPrimary).Range.InsertBefore "Hit Overview / Page "
                .Footers(wdHeaderFooterPrimary).Range.Select
                With Selection
                    If ActiveDocument.Bookmarks.Exists("HitOverviewMac") = True Then
                        .Paragraphs(1).Alignment = wdAlignParagraphCenter
                        .TypeText Text:="Page "
                        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
                            "PAGE ", PreserveFormatting:=True
                        .TypeText Text:=" of "
                        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
                            "NUMPAGES ", PreserveFormatting:=True
                        .EndKey Unit:=wdLine
                        .TypeText Text:=" ~ "
                        ActiveDocument.Hyperlinks.Add Anchor:=.Range, Address:="", _
                        SubAddress:="HitOverview", ScreenTip:="", TextToDisplay:="Hit Overview"
                    Else
                        MsgBox "Bookmark does not exists"
                    End If
                End With
            End With
        Next

        End Sub

【问题讨论】:

  • 编译时有没有报错?在哪一行?什么样的错误(编号和描述)。
  • 谢谢,这是一个愚蠢的错误。没有为超链接对象提供正确的书签名称。

标签: vba hyperlink footer ms-word


【解决方案1】:

好的,这不是宏的问题(以下除外),而是我正在测试的几个文档的问题。 我错过了几个错误 - SubAddress:="BOOKMARK_NAME" AND Anchor:=Selection.Range。

因此,如果任何文档的页脚中已经有一些文本,就会出现问题。所以现在我要先删除页脚。

这里是代码供大家参考-

    Sub InsertFootnote()
    Const wdAlignPageNumberCenter = 1
    Dim varNumberPages As Variant
    varNumberPages = ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)
    If ActiveDocument.Bookmarks.Exists("HitOverviewMac") = True Then
        ActiveDocument.Bookmarks("HitOverviewMac").Delete
    End If
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="HitOverviewMac"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    Dim mHlink As Hyperlink
    Dim i As Long
    For i = 1 To ActiveDocument.Sections.Count
        With ActiveDocument.Sections(i)
            .Footers(wdHeaderFooterPrimary).Range.Text = ""
            .Footers(wdHeaderFooterPrimary).PageNumbers.Add (wdAlignPageNumberCenter)
            .Footers(wdHeaderFooterPrimary).Range.Select
            With Selection
                If ActiveDocument.Bookmarks.Exists("HitOverviewMac") = True Then
                    .Paragraphs.Alignment = wdAlignParagraphCenter
                    .TypeText Text:="Page "
                    .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
                        "PAGE ", PreserveFormatting:=True
                    .TypeText Text:=" of "
                    .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
                        "NUMPAGES ", PreserveFormatting:=True
                    .EndKey Unit:=wdLine
                    .TypeText Text:=" / "
                    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
                    SubAddress:="HitOverviewMac", ScreenTip:="", TextToDisplay:="Hit Overview"
                Else
                    MsgBox "Bookmark does not exists"
                End If
            End With
        End With
    Next

    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2011-10-14
    • 1970-01-01
    • 2018-06-17
    • 2014-05-30
    • 2010-09-22
    • 2021-08-07
    相关资源
    最近更新 更多