我需要在MS Word文档的末尾创建一个索引,列出文档中使用的所有单词,按字母顺序使用的页码。我可以使用内置索引功能吗?如果是这样,我该怎么做呢?或者我需要一个宏,如果是这样,有人可以帮我编写脚本吗?
答案 0 :(得分:2)
这需要大型文档,但会生成在word文档中创建索引所需的索引字段。运行此宏后,您可以References > Insert Index
在文档中包含实际索引。
Dim colWords as Collection
Set colWords = New Colection
'add words you don't want to index
colWords.Add "and"
colWords.Add "you"
Dim wrd As Range
For Each wrd In ActiveDocument.Words
'only if we have 3 chars we index
If Len(Trim(wrd.Text)) > 2 Then
' prevent the field from being Indexed as well...
Dim infield As Boolean
infield = False
Dim fld As Field
For Each fld In ActiveDocument.Fields
If (wrd.Start >= fld.Code.Start And wrd.End <= fld.Code.End) Then
infield = True
Exit For 'break out
End If
Next
If (Not infield) Then
' check if we already indexed?
Dim findWord as String
findWord = LCASE(wrd.Text)
For Each cached in colWords
if cached = findWord Then
infield = True
Exit For 'break out
end If
Next
If (Not infield) Then
ActiveDocument.Indexes.MarkAllEntries Range:=wrd, Entry:=wrd.Text, _
EntryAutoText:=wrd.Text, CrossReference:="", CrossReferenceAutoText:="", _
BookmarkName:="", Bold:=False, Italic:=False
colWords.Add findWord
End If
End If
End If
Next