Thread: [Solved] Find and replace condition
View Single Post
 
Old 07-31-2015, 05:59 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,142
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

The following should do the trick. If case is unimportant remove the part
Code:
, MatchCase:=True
or change it to False

Code:
Sub FindAndReplaceEAwith2()
Dim oRng As Range
Dim oWord As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:="ea", MatchCase:=True)
            Set oWord = oRng.Words(1)
            If oWord.Characters.Last = Chr(32) Then
                oWord.End = oWord.End - 1
            End If
            If Not oRng.Start = oWord.Start Then
                If Not oRng.End = oWord.End Then
                    oRng.Select
                    oRng.Text = 2
                    oRng.Collapse 0
                End If
            End If
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Set oWord = Nothing
    Exit Sub
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