#1
|
|||
|
|||
Find-and-Replace Special Characters
Hello!
I'm having trouble with Find and Replace VBA macros: Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "%" .Replacement.Text = " PERCENT" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAl ' Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "/" .Replacement.Text = " " .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAl It will find the charaters but will not replace them. Please help! |
#2
|
||||
|
||||
The following should work
Code:
Sub Macro1() Dim vFindText As Variant Dim vReplaceText As Variant Dim oRng As Range Dim i As Long vFindText = Array("%", "/") vReplaceText = Array(" PERCENT", " ") For i = 0 To UBound(vFindText) Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting Do While .Execute(findText:=vFindText(i), _ MatchWholeWord:=True, _ Forward:=True, _ Wrap:=wdFindStop) = True oRng.Text = vReplaceText(i) oRng.Collapse wdCollapseEnd Loop End With Next lbl_Exit: Set oRng = 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
|
|||
|
|||
Works!
What would the VB code be if I wanted to run each find/replace separately but in the same macro? I tried this but it fails: Sub Macro1() Dim vFindText As Variant Dim vReplaceText As Variant Dim oRng As Range Dim i As Long vFindText = Array("%") vReplaceText = Array(" PERCENT") For i = 0 To UBound(vFindText) Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting Do While .Execute(findText:=vFindText(i), _ MatchWholeWord:=True, _ Forward:=True, _ Wrap:=wdFindStop) = True oRng.Text = vReplaceText(i) oRng.Collapse wdCollapseEnd Loop End With Next lbl_Exit: Set oRng = Nothing Exit Sub ' Dim vFindText As Variant Dim vReplaceText As Variant Dim oRng As Range Dim i As Long vFindText = Array("/") vReplaceText = Array("' ") For i = 0 To UBound(vFindText) Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting Do While .Execute(findText:=vFindText(i), _ MatchWholeWord:=True, _ Forward:=True, _ Wrap:=wdFindStop) = True oRng.Text = vReplaceText(i) oRng.Collapse wdCollapseEnd Loop End With Next lbl_Exit: Set oRng = Nothing Exit Sub |
#4
|
||||
|
||||
The code I posted runs the searches separately. To do what you suggest requires two macros similar to
Code:
Sub Macro1() Dim sFindText As String Dim sReplaceText As String Dim oRng As Range sFindText = "%" sReplaceText = " PERCENT" Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting Do While .Execute(findText:=sFindText, _ MatchWholeWord:=True, _ Forward:=True, _ Wrap:=wdFindStop) = True oRng.Text = sReplaceText oRng.Collapse wdCollapseEnd Loop End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub Sub Macro2() Dim sFindText As String Dim sReplaceText As String Dim oRng As Range sFindText = "\" sReplaceText = " " Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting Do While .Execute(findText:=sFindText, _ MatchWholeWord:=True, _ Forward:=True, _ Wrap:=wdFindStop) = True oRng.Text = sReplaceText oRng.Collapse wdCollapseEnd Loop End With lbl_Exit: Set oRng = 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 |
#5
|
|||
|
|||
Works Thanks so much!
|
#6
|
|||
|
|||
Can a macro be created that runs other macros?
Sub RunMultipleMacros1() Run Macro1 Run Macro2 Exit Sub End Sub |
#7
|
|||
|
|||
Another issue. I'm using wildcards in the script but it's not working, see below. I've added MatchWildcards:=True, _ runs without errors but replaces with actual characters.
Sub Punctuation() Dim vFindText As Variant Dim vReplaceText As Variant Dim oRng As Range Dim i As Long vFindText = Array(":^tSO ", ". SO ", "? SO ", "WORKERS COMPENSATION", "WORKERS COMP", "WORKER'S COMPENSATION", "WORKER'S COMP") vReplaceText = Array(":^tSO, ", ". SO, ", "? SO, ", "WORKERS' COMPENSATION", "WORKERS' COMP", "WORKERS' COMPENSATION", "WORKERS' COMP") For i = 0 To UBound(vFindText) Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting Do While .Execute(findText:=vFindText(i), _ MatchWholeWord:=True, _ Forward:=True, _ MatchWildcards:=True, _ Wrap:=wdFindStop) = True oRng.Text = vReplaceText(i) oRng.Collapse wdCollapseEnd Loop End With Next lbl_Exit: Set oRng = Nothing Exit Sub End Sub |
#8
|
|||
|
|||
I think I figured out the run multiple macro script:
Sub RunMacros() Application.Run MacroName:=("Macro1") Application.Run MacroName:=("Macro2") Exit Sub End Sub |
#9
|
||||
|
||||
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find/Replace text longer than 255 characters | alex100 | Word VBA | 5 | 07-08-2020 04:14 AM |
Combining Characters in Find & Replace | Surge | Word | 6 | 03-10-2020 12:42 AM |
multiple find and replace in vba for special words | kvnrao | Word VBA | 7 | 11-02-2018 06:28 PM |
Search and Replace special characters between word using wildcards | mauuuuu5 | Word | 1 | 08-23-2015 09:20 PM |
Find and Replace some characters with Bullets | kjxavier | Word | 1 | 01-02-2015 12:15 AM |