Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-27-2025, 03:35 PM
xstranho xstranho is offline Macro for repeated sentences in word Windows 10 Macro for repeated sentences in word Office 2019
Novice
Macro for repeated sentences in word
 
Join Date: Jun 2025
Posts: 5
xstranho is on a distinguished road
Default Macro for repeated sentences in word

A long text, 45 to 50 thousand words. But this text has many insertions, so several sentences are repeated, several paragraphs, with the same content are repeated throughout the text. Is there a way, a macro, to do a cleanup, looking for comparisons between paragraphs throughout the text, and, or perhaps phrases, or sentences, expressions, with more than 5 or six words that are repeated randomly throughout the text, not in the same paragraph, but throughout the text. Sorry, I don't make myself clear. It would be something like this, in paragraph 07 I have ""For Khandahar!", replied the inhabitants, raising their improvised weapons and facing the enemy with a bravery that surprised even themselves." and I know that up ahead, but I don't know in which paragraph there is something similar. How can I create a macro to help me clean up this text. I appreciate all the help.
Reply With Quote
  #2  
Old 06-27-2025, 08:50 PM
macropod's Avatar
macropod macropod is offline Macro for repeated sentences in word Windows 10 Macro for repeated sentences in word Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,387
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So which instance would you want to keep? Do be aware, too, that VBA has no idea what a grammatical sentence is. For example, consider the following:
Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
For you and me, that would count as one sentence; for VBA it counts as 5 sentences.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-28-2025, 06:05 AM
xstranho xstranho is offline Macro for repeated sentences in word Windows 10 Macro for repeated sentences in word Office 2019
Novice
Macro for repeated sentences in word
 
Join Date: Jun 2025
Posts: 5
xstranho is on a distinguished road
Default

I understand, so the ideal would be a macro where I could find throughout the text, a certain number of words that are repeated in text strings. How do I build a macro that does this search and indicates "on page, line such and such, we already have this same sentence". Then it asks, can I delete this sentence? Can you help me with this?
Reply With Quote
  #4  
Old 06-28-2025, 08:34 PM
macropod's Avatar
macropod macropod is offline Macro for repeated sentences in word Windows 10 Macro for repeated sentences in word Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,387
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See: https://www.msofficeforums.com/word-...-document.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old Yesterday, 05:07 AM
gmaxey gmaxey is offline Macro for repeated sentences in word Windows 10 Macro for repeated sentences in word Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,602
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

This is an adaptations of Paul's code that he provided in his linked answer. You can change the value I have "6" words to whatever number you like.


Code:
Sub FindDuplicateStrings()
Application.ScreenUpdating = False
Dim i As Long, RngSrc As Range, RngFnd As Range
Dim oRng As Range
Const Clr As Long = wdBrightGreen
Dim eTime As Single
eTime = Timer
Options.DefaultHighlightColorIndex = Clr
Dim oCol As New Collection
With ActiveDocument
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Set RngSrc = .Range
  RngSrc.End = .Words(6).End  'Change "6" to a number to suit.
  MsgBox RngSrc.Text
  Do
    i = i + 1
    If i Mod 100 = 0 Then DoEvents
    On Error Resume Next
    'If RngSrc.HighlightColorIndex <> Clr Then
      Set RngFnd = .Range(RngSrc.End, .Range.End)
      If Len(RngSrc.Text) < 256 Then
        With RngFnd.Find
          .Text = RngSrc.Text
          .Replacement.Text = "^&"
          .Replacement.Highlight = True
          .Wrap = wdFindStop
          .Execute Replace:=wdReplaceAll
        End With
      Else
        With RngFnd
          With .Find
            .Text = Left(RngSrc.Text, 255)
            .Wrap = wdFindStop
            .Execute
          End With
          Do While .Find.Found
            If RngSrc.Text = .Duplicate.Text Then
              RngSrc.HighlightColorIndex = Clr
              .Duplicate.HighlightColorIndex = Clr
            End If
            .Collapse wdCollapseEnd
            .Find.Execute
          Loop
        End With
      End If
    'End If
    RngSrc.MoveStart wdWord, 1
    RngSrc.MoveEnd wdWord, 1
    Loop Until RngSrc.End = .Range.End
End With
' Report time taken. Elapsed time calculation allows for execution to extend past midnight.
MsgBox "Finished. Elapsed time: " & (Timer - eTime + 86400) Mod 86400 & " seconds."
Application.ScreenUpdating = True
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
macro, repetead, sentences



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for repeated sentences in word Macro to select multiple sentences that contain a specific word MauiTruss Word VBA 7 10-03-2018 03:46 PM
Macro for repeated sentences in word Macro to number sentences tjf816 Word VBA 10 03-29-2017 05:42 PM
Macro for repeated sentences in word repeated formatting with macro in Word Pompidou Word VBA 7 11-14-2016 10:27 AM
Macro to highlight repeated words in word file and extract into excel file aabri Word VBA 1 06-14-2015 07:20 AM
Macro for repeated sentences in word Need a Macro that Combines Every 5 sentences into a paragraph jgarland Word 22 01-11-2012 11:19 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:46 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft