Highlight (not delete) repeat sentences or phrases

前端 未结 3 831
故里飘歌
故里飘歌 2020-12-28 10:03

I am getting the impression that this is not possible in word but I figure if you are looking for any 3-4 words that come in the same sequence anywhere in a very long paper

3条回答
  •  暖寄归人
    2020-12-28 10:50

    To highlight all duplicate sentences, you can also use ActiveDocument.Sentences(i). Here is an example

    LOGIC

    1) Get all the sentences from the word document in an array

    2) Sort the array

    3) Extract Duplicates

    4) Highlight duplicates

    CODE

    Option Explicit
    
    Sub Sample()
        Dim MyArray() As String
        Dim n As Long, i As Long
        Dim Col As New Collection
        Dim itm
    
        n = 0
        '~~> Get all the sentences from the word document in an array
        For i = 1 To ActiveDocument.Sentences.Count
            n = n + 1
            ReDim Preserve MyArray(n)
            MyArray(n) = Trim(ActiveDocument.Sentences(i).Text)
        Next
    
        '~~> Sort the array
        SortArray MyArray, 0, UBound(MyArray)
    
        '~~> Extract Duplicates
        For i = 1 To UBound(MyArray)
            If i = UBound(MyArray) Then Exit For
            If InStr(1, MyArray(i + 1), MyArray(i), vbTextCompare) Then
                On Error Resume Next
                Col.Add MyArray(i), """" & MyArray(i) & """"
                On Error GoTo 0
            End If
        Next i
    
        '~~> Highlight duplicates
        For Each itm In Col
            Selection.Find.ClearFormatting
            Selection.HomeKey wdStory, wdMove
            Selection.Find.Execute itm
            Do Until Selection.Find.Found = False
                Selection.Range.HighlightColorIndex = wdPink
                Selection.Find.Execute
            Loop
        Next
    End Sub
    
    '~~> Sort the array
    Public Sub SortArray(vArray As Variant, i As Long, j As Long)
      Dim tmp As Variant, tmpSwap As Variant
      Dim ii As Long, jj As Long
    
      ii = i: jj = j: tmp = vArray((i + j) \ 2)
    
      While (ii <= jj)
         While (vArray(ii) < tmp And ii < j)
            ii = ii + 1
         Wend
         While (tmp < vArray(jj) And jj > i)
            jj = jj - 1
         Wend
         If (ii <= jj) Then
            tmpSwap = vArray(ii)
            vArray(ii) = vArray(jj): vArray(jj) = tmpSwap
            ii = ii + 1: jj = jj - 1
         End If
      Wend
      If (i < jj) Then SortArray vArray, i, jj
      If (ii < j) Then SortArray vArray, ii, j
    End Sub
    

    SNAPSHOTS

    BEFORE

    enter image description here

    AFTER

    enter image description here

提交回复
热议问题