View Single Post
 
Old 01-07-2018, 03:55 PM
puff puff is offline Windows 7 64bit Office 2013
Advanced Beginner
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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
It's actually more complicated than this since the document actually doesn't have "|" to indicate where the repeat starts. Although I can somehow ask the software to insert a "|" before the repeat, it's not promising. So that is why I want to use a combination words rather than just 1 word for comparing.
Reply With Quote