突出显示两个 Word 文件之间的相似文本

突出显示两个 Word 文件之间的相似文本

我有两个 Word 文档,我想突出显示文件 2 上的共同文本。我有下面的宏(它应该突出显示文档之间有超过 4 个共同单词的任何文本字符串),但它不起作用。(顺便说一句,我确实用第二个文件的正确路径替换了文件路径。)非常感谢任何建议。


Sub HighlightCommonWords()
    Dim doc1 As Document
    Dim doc2 As Document
    Dim wrd1 As Range
    Dim wrd2 As Range
    Dim i As Long
    Dim j As Long
    Dim common As String
    
    Set doc1 = ActiveDocument
    Set doc2 = Documents.Open("path\to\second\file.docx")
    
    For i = 1 To doc1.Words.Count
        Set wrd1 = doc1.Words(i)
        If wrd1.Words.Count > 4 Then
            For j = 1 To doc2.Words.Count
                Set wrd2 = doc2.Words(j)
                If wrd2.Words.Count > 4 Then
                    If wrd1.Text = wrd2.Text Then
                        common = wrd1.Text
                        Do While wrd1.Find.Execute(FindText:=common, MatchCase:=True)
                            wrd1.HighlightColorIndex = wdGray25
                        Loop
                    End If
                End If
            Next j
        End If
    Next i
    
    doc2.Close
End Sub

相关内容