【发布时间】: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