#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
The following should do the trick. If case is unimportant remove the part
Code:
, MatchCase:=True 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 |
#3
|
|||
|
|||
Sincere thanks Graham. It does exactly what I wanted. I really appreciate your help.
Norman |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 what box in Find and replace limits the length of a search term | Hoxton118 | Word VBA | 7 | 06-10-2014 05:05 AM |
Macro - replace with condition | ubns | Word VBA | 1 | 05-02-2012 12:52 AM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |
Help with find and replace or query and replace | shabbaranks | Excel | 4 | 03-19-2011 08:38 AM |