2013-07-21 50 views
1

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

回答

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 
+0

谢谢Rene ..会看看! – Craig

+0

有没有办法来加速这件事,因为它在我的机器上与86页文档崩溃? – Craig

+0

我添加了一个可怜的人缓存集合,该集合已经为此次运行保存了索引字... – rene

相关问题