Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-27-2023, 05:36 AM
davidvargas72 davidvargas72 is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2021
Novice
Find-and-Replace Special Characters
 
Join Date: Jan 2023
Posts: 6
davidvargas72 is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 01-27-2023, 05:56 AM
gmayor's Avatar
gmayor gmayor is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
Reply With Quote
  #3  
Old 01-27-2023, 07:38 AM
davidvargas72 davidvargas72 is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2021
Novice
Find-and-Replace Special Characters
 
Join Date: Jan 2023
Posts: 6
davidvargas72 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 01-27-2023, 10:17 PM
gmayor's Avatar
gmayor gmayor is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
Reply With Quote
  #5  
Old 01-28-2023, 06:52 AM
davidvargas72 davidvargas72 is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2021
Novice
Find-and-Replace Special Characters
 
Join Date: Jan 2023
Posts: 6
davidvargas72 is on a distinguished road
Default

Works Thanks so much!
Reply With Quote
  #6  
Old 01-28-2023, 06:58 AM
davidvargas72 davidvargas72 is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2021
Novice
Find-and-Replace Special Characters
 
Join Date: Jan 2023
Posts: 6
davidvargas72 is on a distinguished road
Default

Can a macro be created that runs other macros?

Sub RunMultipleMacros1()
Run Macro1
Run Macro2
Exit Sub
End Sub
Reply With Quote
  #7  
Old 01-28-2023, 08:43 AM
davidvargas72 davidvargas72 is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2021
Novice
Find-and-Replace Special Characters
 
Join Date: Jan 2023
Posts: 6
davidvargas72 is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 01-28-2023, 09:44 AM
davidvargas72 davidvargas72 is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2021
Novice
Find-and-Replace Special Characters
 
Join Date: Jan 2023
Posts: 6
davidvargas72 is on a distinguished road
Default

I think I figured out the run multiple macro script:

Sub RunMacros()
Application.Run MacroName:=("Macro1")
Application.Run MacroName:=("Macro2")
Exit Sub
End Sub
Reply With Quote
  #9  
Old 01-28-2023, 10:03 PM
gmayor's Avatar
gmayor gmayor is offline Find-and-Replace Special Characters Windows 10 Find-and-Replace Special Characters Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

See Replace using wildcards
__________________
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find-and-Replace Special Characters Find/Replace text longer than 255 characters alex100 Word VBA 5 07-08-2020 04:14 AM
Find-and-Replace Special Characters Combining Characters in Find & Replace Surge Word 6 03-10-2020 12:42 AM
Find-and-Replace Special Characters multiple find and replace in vba for special words kvnrao Word VBA 7 11-02-2018 06:28 PM
Find-and-Replace Special Characters Search and Replace special characters between word using wildcards mauuuuu5 Word 1 08-23-2015 09:20 PM
Find-and-Replace Special Characters Find and Replace some characters with Bullets kjxavier Word 1 01-02-2015 12:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:58 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