How to find and highlight duplicate paragraphs in Word document?

Supposing, you have a large Word document which may have hundreds of pages, now, you want to check if there are duplicate paragraphs and then highlight to make them outstanding, so that you can deal with the duplicate sentences. How could you find and highlight the duplicate paragraphs quickly and easily in Word document?

Find and highlight the duplicate paragraphs in Word document with VBA code


Find and highlight the duplicate paragraphs in Word document with VBA code

To find and highlight the duplicate paragraphs in a Word document, the following VBA code can do you a favor, please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. And then, click Insert > Module, copy and paste below code into the opened blank module:

VBA code: Find and highlight the duplicate paragraphs in Word document:

Sub highlightdup()
    Dim I, J As Long
    Dim xRngFind, xRng As Range
    Dim xStrFind, xStr As String
    Options.DefaultHighlightColorIndex = wdYellow
    Application.ScreenUpdating = False
    With ActiveDocument
        For I = 1 To .Paragraphs.Count - 1
            Set xRngFind = .Paragraphs(I).Range
            If xRngFind.HighlightColorIndex <> wdYellow Then
                For J = I + 1 To .Paragraphs.Count
                    Set xRng = .Paragraphs(J).Range
                    If xRngFind.Text = xRng.Text Then
                        xRngFind.HighlightColorIndex = wdBrightGreen
                        xRng.HighlightColorIndex = wdYellow
                    End If
                Next
            End If
        Next
    End With
End Sub

3. And then press F5 key to run this code, all the duplicate sentences are highlighted at once, the first displayed duplicate paragraphs are highlighted with green color, and other duplicates are highlighted with yellow color, see screenshot:

doc highlight dup sentences 1



Recommended Word Productivity Tools

 

Kutools For Word - More Than 100 Advanced Features For Word, Save Your 50% Time

  • Complicated and repeated operations can be done one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders into one with your desired order.
  • Split the current document into separate documents according to heading, section break or other criteria.
  • Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Tom · 1 months ago
    Tried this for my book in MS Word. First, it would not work because I had bullet points. I removed them and then it only found 2 instances "blank page" and "table of contents". I purposely have several sentences repeated, and this macro did not find them. Thank you for trying, but I would say this doesn't work.

  • To post as a guest, your comment is unpublished.
    Peter · 2 months ago
    I had a very long document to process, the code above would take at least 100 days to finish and blocked everything while working at it. The main culprit is the "Set xRng = .Paragraphs(J).Range" which is very slow. I did an alternative version which ran in just 4 hours and presents a continuous report on the processing status and time to end. (To see the report in real time you have to open the "immediate window" by pressing Ctrl+G in the Microsoft Visual Basic for Applications window.) The code works well, except that it predicts a longer time to end than is actually the case (depends on the document). The code is as follows:

    Sub highlightdup()
    Dim StartTime, SecondsElapsed As Date
    Dim secondsPerComparison As Double
    Dim I, J, PC, totalComparisons, comparisonsDone, C, secondsToFinish As Long
    Dim xRngFind, xRng As Range
    Dim xStrg, minutesToFinish As String
    Dim currentParag, nextParag As Paragraph
    'Options.DefaultHighlightColorIndex = wdYellow
    Application.ScreenUpdating = False
    With ActiveDocument
    StartTime = Now()
    C = 0
    PC = .Paragraphs.Count
    totalComparisons = CLng((PC * (PC + 1)) / 2)
    Set currentParag = .Paragraphs(1)
    For I = 1 To PC - 1
    'Debug.Print "processing paragraph " & I & " of a total of " & PC & " " & currentParag.Range.Text
    'Debug.Print Len(currentParag) & currentParag
    If currentParag.Range.HighlightColorIndex <> wdYellow Then
    If currentParag.Range.HighlightColorIndex <> wdBrightGreen Then
    Set nextParag = currentParag
    For J = I + 1 To PC
    Set nextParag = nextParag.Next
    If currentParag.Range.Text = nextParag.Range.Text Then
    currentParag.Range.HighlightColorIndex = wdBrightGreen
    nextParag.Range.HighlightColorIndex = wdYellow
    Debug.Print "found one!! " & " I = " & I & " J = " & J & nextParag.Range.Text
    End If
    Next
    End If
    End If
    DoEvents
    comparisonsDone = PC * (I - 1) + (J - I)
    SecondsElapsed = DateDiff("s", StartTime, Now())
    secondsPerComparison = CLng(SecondsElapsed) / comparisonsDone
    secondsToFinish = CLng(secondsPerComparison * (totalComparisons - comparisonsDone))
    minutesToFinish = Format(secondsToFinish / 86400, "hh:mm:ss")
    elapsedTime = Format(SecondsElapsed / 86400, "hh:mm:ss")
    Debug.Print "Finished procesing paragraph " & I & " of " & PC & ". Elapsed time = " & elapsedTime & ". Time to finish = " & minutesToFinish
    Set currentParag = currentParag.Next
    Next
    End With
    End Sub
  • To post as a guest, your comment is unpublished.
    helin · 4 months ago
    Hello

    Thank you for the helping

    But how can I find the same sentences in my text?

    Regards
  • To post as a guest, your comment is unpublished.
    Helin · 4 months ago
    Hello

    Thank you for the helping

    But how can I find the same sentences in my text?

    Regards
  • To post as a guest, your comment is unpublished.
    Alex · 1 years ago
    It throws Compile error: Syntax error, the code is 100% as the example.
  • To post as a guest, your comment is unpublished.
    dvid · 2 years ago
    I tried, and it works fine!
    I just wanted to know - could I do the same thing - but not a whole paragraph, but a sentence with a few words I'd set up - like 10 words?
  • To post as a guest, your comment is unpublished.
    Juliana · 2 years ago
    Tentei e não funcionou, só da que parou de funcionar.