View Single Post
 
Old 01-07-2018, 07:52 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Without access to the text the following should work - at least it does with your example.

Basically it looks for the " | " string which separates the similar texts, then identifies the next word, then deletes all the previous words until the identified next word is found. It then deletes the previous word (which is the repeated identified word) and then sets the found string to a single space before looking for the next " | " string.

A big problem is that what Word sees as a 'word' may not be what you think of as a 'word' and punctuation in the strings can screw it all up.

Code:
Sub Macro1()
Dim orng As Range
Dim orngA As Range
    On Error GoTo err_Handler
    Set orng = ActiveDocument.Range
    With orng.Find
        Do While .Execute(FindText:=" | ")
            Set orngA = orng.Next.Words.First
            Do Until orng.Previous.Words.Last = orngA
                orng.Previous.Words.Last.Delete
            Loop
            orng.Previous.Words.Last.Delete
            orng.Text = " "
            orng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set orng = Nothing
    Set orngA = Nothing
    Exit Sub
err_Handler:
    Err.Clear
    GoTo lbl_Exit
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote