我正在嘗試創建一個宏,用於Microsoft Word 2007中,該宏將搜索位於外部Excel文件中的多個關鍵字(字符串變量)的文檔(使其在外部文件中的條件是經常被改變和更新)。我已經想出瞭如何逐段搜索一個文檔併爲該詞的每個實例着色,並且我假設正確的方法是使用動態數組作爲搜索詞變量。在VBA中搜索文檔中的多個術語?
問題是:我如何讓宏創建一個包含來自外部文件的所有術語的數組並搜索每個術語的每個段落?
這是我到目前爲止有:
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
爲尋找謝謝!