View Single Post
 
Old 11-12-2021, 01:02 AM
Bikram Bikram is offline Windows 10 Office 2007
Advanced Beginner
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default find and replace only in current selection

Greeting to all,
I have been using find and replace through vba. It works fine except, sometimes it replaces whole document instead of just replacing the selection. The code I am using is below:
Sub Macro32()
'
' Macro32 Macro
'
'

With selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
End with
With Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
End with
With Selection.Find
.Text = "(=)"
.Replacement.Text = "^t\1"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

How could I modify this code to just replace the selected area and nothing else? Looking forward for suggestions. Thanks in advance.
Reply With Quote