如何在 Microsoft Word 中搜索和突出显示多个术语?

How do I search and highlight multiple terms in Microsoft Word?

我的目标是能够 运行 这个脚本并让文档搜索并突出显示一定数量的术语,通常是 10 个以上的术语。我想出了如何使用 我在这里找到的方法,但每次我使用它时 Word 都会崩溃。

下面是我从网上找到的不同 forums/videos 拼凑而成的一个更简单的版本。它完全符合我的要求 除了 我不知道如何让它查找多个术语。

.Text = "Text" 效果很好,但只适用于一个学期。如果我列出多个,那么它只会查找我最后列出的那个。我已经测试了我在网上找到的其他代码块,但我无法弄明白。

我希望这是一个简单的修复,特别是因为其余代码可以满足我的要求。 TIA!


Sub UsingTheFindObject_Medium()

'Declare Variables.
Dim wrdFind As Find
Dim wrdRng As range
Dim wrdDoc As Document

'Grab the ActiveDocument.
Set wrdDoc = Application.ActiveDocument

'Define the Content in the document
Set wrdRng = wrdDoc.Content

'Define the Find Object based on the Range.
Set wrdFind = wrdRng.Find

'Define the parameters of the Search.
With wrdFind
    
'Search the text for the following term(s)
.Text="Test"

.Format = True

.MatchCase = False

.MatchWholeWord = True

.MatchWildcards = False

.MatchSoundsLike = False

.MatchAllWordForms = False

End With

Do While wrdFind.Execute = True
    
    'Change the color to Yellow.
    wrdRng.HighlightColorIndex = wdYellow
    
Loop

End Sub

将您当前的例程用作函数。

这是一个例子。

Function FindAndMark(sText As String) ' UsingTheFindObject_Medium()
' 
' Charles Kenyon
'Declare Variables.
    Dim wrdFind As Find
    Dim wrdRng As Range
    Dim wrdDoc As Document

    'Grab the ActiveDocument.
    Set wrdDoc = Application.ActiveDocument

    'Define the Content in the document
    Set wrdRng = wrdDoc.Content

    'Define the Find Object based on the Range.
    Set wrdFind = wrdRng.Find

    'Define the parameters of the Search.
    With wrdFind
        'Search the text for the following term(s)
        .Text = sText
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    '  Mark text
    Do While wrdFind.Execute = True
        'Change the color to Yellow.
        wrdRng.HighlightColorIndex = wdYellow
    Loop
    Set wrdFind = Nothing
    Set wrdRng = Nothing
    Set wrdDoc = Nothing
End Function

Sub MultiFindMark()
    ' 
    ' Charles Kenyon
    Dim i As Integer
    Const n As Integer = 4  ' set number (n) of terms in search
    Dim sArray(n) As String ' Create array to hold terms
    ' Assign values, starting at 0 and going to n-1
    Let sArray(0) = "Aenean"
    Let sArray(1) = "Pellentesque"
    Let sArray(2) = "libero"
    Let sArray(3) = "pharetra"
    For i = 0 To n - 1
        FindAndMark (sArray(i))
    Next i
    
End Sub

这是使用 ASH 中的代码处理数组的修订版

Sub MultiFindMark2() 
    ' 
    ' Charles Kenyon
    ' modified to use methods proposed by ASH
    Dim i As Long
    Dim sArray() As String ' Create array to hold terms
    ' Assign values, starting at 0 and going to n-1
    sArray = Split("Aenean Pellentesque libero pharetra")    ' your list separated by spaces
    For i = 0 To UBound(sArray)
        FindAndMark (sArray(i))
    Next i
    
End Sub

部分更改显示为评论:

Sub MultiFindMark2() 
    ' 
    ' Charles Kenyon
    ' modified to use methods proposed by ASH
    Dim i As Long
'    Const n As Integer = 4  ' set number (n) of terms in search
    Dim sArray() As String ' Create array to hold terms
    ' Assign values, starting at 0 and going to n-1
    sArray = Split("Aenean Pellentesque libero pharetra")    ' your list separated by spaces
'    Let sArray(0) = "Aenean"
'    Let sArray(1) = "Pellentesque"
'    Let sArray(2) = "libero"
'    Let sArray(3) = "pharetra"
    For i = 0 To UBound(sArray)
        FindAndMark (sArray(i))
    Next i
    
End Sub

注意,这个还是需要函数的。

这会做你想做的事。

Sub HighlightMultipleWords()

Dim sArr() As String
Dim rTmp As Range
Dim x As Long
sArr = Split("highlight specific words") ' your list
Options.DefaultHighlightColorIndex = wdYellow

For x = 0 To UBound(sArr)
Set rTmp = ActiveDocument.Range
    With rTmp.Find
                .Text = sArr(x)
                .Replacement.Text = sArr(x)
                .Replacement.Highlight = True
                .Execute Replace:=wdReplaceAll
    End With
Next

End Sub

之前:

之后: