我有几个快速宏,一个用于将文本标记为项目并为其添加书签,另一个用于收集索引中的所有书签。插入新文本时(将超链接切换回书签),新段落位于最后段落之前并反转顺序。
我在这里缺少什么?
Attribute VB_Name = "Project_Bookmarks_TOC"
Option Explicit
Const Prj As String = "PROJECT_"
Private Sub Bookmark_Generate_List()
Dim bkm As BookMark
Dim newParagraph As Range
Dim documentEnd As Range
Dim strOut As String
' Create a new Range object at the end of the document
Set documentEnd = ActiveDocument.Range
documentEnd.Collapse Direction:=wdCollapseEnd
For Each bkm In ActiveDocument.Bookmarks
If Left(LCase(Trim(bkm.Name)), Len(Prj)) = LCase(Prj) Then
' Create a new paragraph for each bookmark
Set newParagraph = documentEnd.Duplicate
newParagraph.Collapse Direction:=wdCollapseEnd
newParagraph.Text = bkm.Name & vbTab & bkm.Range & vbTab & bkm.Range.Information(wdActiveEndPageNumber) & vbCr
' Add hyperlink to the bookmark
ActiveDocument.Hyperlinks.Add _
Anchor:=newParagraph, _
Address:="", _
SubAddress:=bkm.Name, _
TextToDisplay:=bkm.Name & vbTab & bkm.Range & vbTab & bkm.Range.Information(wdActiveEndPageNumber) & vbCr
' Move the cursor to the end of the document
newParagraph.Collapse Direction:=wdCollapseEnd
End If
Next bkm
' Format the last paragraph as needed
With newParagraph.ParagraphFormat
.Alignment = wdAlignParagraphLeft ' Set alignment to left
' Add more formatting options as needed
End With
End Sub
根据文档:
[书签顺序]
对象,索引号表示书签在书签对话框框中按字母顺序排列的书签列表中的位置(单击“名称”可按字母顺序对书签列表进行排序)。Document
[书签顺序]看下面的例子: 使用您的代码,它将返回:
Selection
或Range
对象 [...] 索引号代表书签的位置
ABookmark
、
DBookmark
、
ZBookmark
。
For Each bkm In ActiveDocument.Bookmarks
MsgBox "Doc: " & bkm
Next bkm
使用文档中的范围,它将返回 ZBookmark
、
ABookmark
、
DBookmark
For Each bkm In ActiveDocument.Range(Start:=0).Bookmarks
MsgBox "Range: " & bkm
Next bkm
应该有一个 API wdSortByLocation
,但就像这个问题表明,它在我的测试中不起作用。