Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2015, 12:33 AM
norgro norgro is offline Find and replace condition Windows XP Find and replace condition Office 2007
Novice
Find and replace condition
 
Join Date: Feb 2013
Location: Perh Australia
Posts: 12
norgro is on a distinguished road
Default Find and replace condition

I want to apply a condition to the replacement.

I want "ea" to be replaced by "2" only when "ea " is not the first or last letters of the word that contains it.

1. How do I apply this condition?
2. How do I get the find and replace to stop when the end of document is reached?

Note: In Braille, the code for "ea" is "2" in a word document.

Help would be greatly appreciated.

Norman

Sub FindAndReplaceEAwith2()
'
' Find and Replace "ea" with "2" Macro
' Macro recorded 31/7/15 by NG
'
On Error GoTo StopMacro
Set oRng = ActiveDocument.Range
oRng.HighlightColorIndex = wdNoHighlight
oRng.Find.ClearFormatting
oRng.Find.Replacement.ClearFormatting
oRng.Find.Replacement.Highlight = True
For i = 1 To 20
With oRng.Find
.Text = "ea"
.Replacement.Text = "2"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False


End With
oRng.Find.Execute Replace:=wdReplaceOne
MsgBox (oRng.Characters.First.Previous)
Next i

StopMacro:
Stop

End Sub
Reply With Quote
  #2  
Old 07-31-2015, 05:59 AM
gmayor's Avatar
gmayor gmayor is offline Find and replace condition Windows 7 64bit Find and replace condition Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
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 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
  #3  
Old 07-31-2015, 06:38 AM
norgro norgro is offline Find and replace condition Windows XP Find and replace condition Office 2007
Novice
Find and replace condition
 
Join Date: Feb 2013
Location: Perh Australia
Posts: 12
norgro is on a distinguished road
Default

Sincere thanks Graham. It does exactly what I wanted. I really appreciate your help.

Norman
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace condition VBA Batch Find & Replace for all MSOffice extensions, to replace File Name and Content of the File QA_Compliance_Advisor Word VBA 11 09-11-2014 11:51 PM
Find and replace condition Find what box in Find and replace limits the length of a search term Hoxton118 Word VBA 7 06-10-2014 05:05 AM
Find and replace condition Macro - replace with condition ubns Word VBA 1 05-02-2012 12:52 AM
Find and replace condition Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Find and replace condition Help with find and replace or query and replace shabbaranks Excel 4 03-19-2011 08:38 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:14 PM.


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