【发布时间】:2010-03-19 07:00:53
【问题描述】:
我正在尝试创建一个在 Microsoft Word 2007 中使用的宏,它将在文档中搜索位于外部 Excel 文件中的多个关键字(字符串变量)(将其放在外部文件中的原因是术语经常会更改和更新)。我已经弄清楚如何逐段搜索文档以查找单个术语并为该术语的每个实例着色,并且我认为正确的方法是使用动态数组作为搜索术语变量。
问题是:如何让宏创建一个包含外部文件中所有术语的数组并在每个段落中搜索每个术语?
这是我目前所拥有的:
Sub SearchForMultipleTerms()
'
Dim SearchTerm As String 'declare search term
SearchTerm = InputBox("What are you looking for?") 'prompt for term. this should be removed, as the terms should come from an external XLS file rather than user input.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatti…
With Selection.Find
.Text = SearchTerm 'find the term!
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
While Selection.Find.Execute
Selection.GoTo What:=wdGoToBookmark, Name:="\Para" 'select paragraph
Selection.Font.Color = wdColorGray40 'color paragraph
Selection.MoveDown Unit:=wdParagraph, Count:=1 'move to next paragraph
Wend
End Sub
感谢收看!
【问题讨论】: