3

我需要在 MS Word 文档的末尾创建一个索引,列出文档中使用的所有单词,按字母顺序使用的页码。我可以使用内置的索引功能来做到这一点吗?如果是这样,我该怎么做?或者我是否需要一个宏,如果需要,有人可以帮我编写脚本吗?

4

1 回答 1

3

这永远需要大型文档,但确实会生成您需要在 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
于 2013-07-21T18:01:51.063 回答