从核对清单中突出显示单词Microsoft Word,并突出显示核对清单中的匹配单词

时间:2018-03-15 05:51:28

标签: vba ms-word word-vba

我使用Macro从清单中查找单词并在Word文档中突出显示。但如果从列表中找不到单词那么。我还想突出检查清单中的匹配单词,以便我知道找到这些单词并且缺少这些单词。

还有一个它还在单词内搜索也需要修改的单词。即 如果在清单中我有单词Country但它也找到Count,我想要找到整个单词。但第一个问题是最重要的。

Sub CompareWordList1()
    Dim sCheckDoc As String
    Dim docRef As Document
    Dim docCurrent As Document
    Dim wrdRef As Object

    sCheckDoc = "D:\List.docx"
    Set docCurrent = Selection.Document
    Set docRef = Documents.Open(sCheckDoc)
    docCurrent.Activate

    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Font.Bold = True
        .Replacement.Font.ColorIndex = wdDarkRed
        .Replacement.Text = "^&"
        .Forward = True
        .Format = True
        .MatchWholeWord = True
        .MatchCase = True
        .MatchWildcards = False
    End With

    For Each wrdRef In docRef.Words
        If Asc(Right(wrdRef, 1)) > 32 Then
            With Selection.Find
                .Wrap = wdFindContinue
                .Text = wrdRef
                .Execute Replace:=wdReplaceAll
            End With
        End If
    Next wrdRef

    docRef.Close
    docCurrent.Activate
End Sub

1 个答案:

答案 0 :(得分:0)

关键是 "@angular/cli": "~1.7.1", "@angular/compiler-cli": "^5.2.0", 如果操作成功则返回.Execute Replace:=wdReplaceAll,如果没有替换则True。所以我们可以用它来确定这个单词是否被找到。

因此我们可以编写一个突出文档中一个单词的函数,以便我们可以将该函数重用于不同的单词和不同的文档:

False

然后我们可以使用该函数替换特定文档中的单词,如果单词被替换,则返回Option Explicit Public Function HighlightOneWordInDocument(DocToHighlight As Document, ByVal WordToHighlight As String) As Boolean If Len(WordToHighlight) = 0 Then Exit Function 'exit if no WordToHighlight is empty otherwise below if fails If Asc(Right(WordToHighlight, 1)) > 32 Then With DocToHighlight.Content.Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Font.Bold = True .Replacement.Font.ColorIndex = wdDarkRed .Replacement.Text = "^&" .Forward = True .Format = True .MatchWholeWord = True .MatchCase = True .MatchWildcards = False .Wrap = wdFindContinue .Text = WordToHighlight HighlightOneWordInDocument = .Execute(Replace:=wdReplaceAll) End With End If End Function

True

所以我们只需要记住在数组中找到的单词,这样我们就可以使用该数组来突出显示单词列表:

HighlightOneWordInDocument(docCurrent, wrdRef) 
  'returns true if wrdRef was replaced in docCurrent