Restrict Find&Repalce to Selected Text

T

TT

I would like to write a macro to do multiple Find/replace operations but I
want the operations confined to a block of text selected by the user. Can
anyone point me to an example or sketch the approach?

Thanks!
 
V

Vince

Somthing like this will be needed.

After the user has selected the text:

Dim R1 as range
set R1=selection.range ' Assign R1 to be the range of the selected text

with R1.find ' you will find only in the selected text
.text="Blah"

with .replacement
.text="Blah2"
end with
.execute replace:=wdreplaceall
end with
 
J

Jay Freedman

I would like to write a macro to do multiple Find/replace operations but I
want the operations confined to a block of text selected by the user. Can
anyone point me to an example or sketch the approach?

Thanks!

The key to this is to use a Range object to do the search in a loop,
and check each iteration of the loop to make sure the range is still
within the selected area:

Sub foo()
Dim oRg As Range

Set oRg = Selection.Range
With oRg.Find
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Text = "dolor"
.Replacement.Text = "dollar"

Do While .Execute(Replace:=wdReplaceOne) _
And oRg.InRange(Selection.Range)
' InRange is true if oRg is inside Selection.Range
Loop
End With
End Sub
 
G

Greg Maxey

Jay,

My test of the code you posted isn't working.

I typed:

Now is the time for all good men.

Now is the time for all good men.

Then selected the first.

Running

Sub FRinSelectionOnly()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Text = "time"
.Replacement.Text = "hour"
Do While .Execute(Replace:=wdReplaceOne) And oRg.InRange(Selection.Range)
Loop
End With
End Sub

Resulted in "time" being replaced by "hour" in both the selected and
non-selected sentences.

I changed your code a bit to:

Sub FRinSelectionOnly1()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
.Text = "time"
.Replacement.Text = "hour"
Do While .Execute(Replace:=wdReplaceAll)
Loop
End With

Using wdFindStop vice wdFindContinue resulted in only the first (and
selected) sentence being altered.
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Jay,

My test of the code you posted isn't working.

I typed:

Now is the time for all good men.

Now is the time for all good men.

Then selected the first.

Running

Sub FRinSelectionOnly()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Text = "time"
.Replacement.Text = "hour"
Do While .Execute(Replace:=wdReplaceOne) And
oRg.InRange(Selection.Range) Loop
End With
End Sub

Resulted in "time" being replaced by "hour" in both the selected and
non-selected sentences.

I changed your code a bit to:

Sub FRinSelectionOnly1()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = False
.Text = "time"
.Replacement.Text = "hour"
Do While .Execute(Replace:=wdReplaceAll)
Loop
End With

Using wdFindStop vice wdFindContinue resulted in only the first (and
selected) sentence being altered.

This works:

Dim oRg As Range

Set oRg = Selection.Range
With oRg.Find
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Text = "time"
Do While .Execute And oRg.InRange(Selection.Range)
oRg.Text = "hour"
Loop
End With

There something I am not quite getting with the range find/replace.... Jay's
code should work, but it does not, and as you discovered, if you use
wdFindStop, it replaces only the first occurrence... I know I got that to
work once... but it is way too late for me to dig it up right now, tomorrow
maybe?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi everybody,
this one works for me:

Sub test4444()
Dim rTmp As Range
Set rTmp = Selection.Range
ResetSearch
With rTmp.Find
.Text = "width"
.Replacement.Text = "breite"
.Execute Replace:=wdReplaceAll
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if required
.Execute
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
G

Greg Maxey

Helmut,

Yes it does. Why does it work this doesn't is very puzzling:

Sub FRinSelectionOnly()
Dim oRg As Range
Set oRg = Selection.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
.Text = "time"
.Replacement.Text = "hour"
.Execute Replace:=wdReplaceAll
End With
End Sub
 
H

Helmut Weber

Hi Greg,
Yes it does. Why does it work this doesn't is very puzzling:

Yes, indeed.
An additional .Wrap = wdFindStop helps.
.Execute
.Text = "time"
.Replacement.Text = "hour" .Wrap = wdFindStop
.Execute Replace:=wdReplaceAll

But this is not meant to be an explanation,
maybe a hint to someone who can explain.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg

Helmut,

It appears the quirk (that's what I will call it) is in the .Wrap code.

Take your code without the Reset routines and with two .Wrap lines
(both stetted out)

Sub FRinSelectionOnlyC()
Dim oRng As Range
Set oRng = Selection.Range
With oRng.Find
'.Wrap = wdFindContinue
'.Wrap = wdFindStop
.Text = "time"
.Replacement.Text = "hour"
.Execute Replace:=wdReplaceAll
End With
End Sub

It works.

Activate .Wrap wdFindContinue it fails
Deactive .Wrap wdFindContinue it works
Activate .Wrap wdFindStop it works
Deactivate .Wrap wdFindStop it works

Remove both and it works.

Sub FRinSelectionOnlyC()
Dim oRng As Range
Set oRng = Selection.Range
With oRng.Find
.Text = "time"
.Replacement.Text = "hour"
.Execute Replace:=wdReplaceAll
End With
End Sub

It appears that you don't need .Wrap wdFindStop for it to work but it
won't work with .Wrap wdFindContinue.
 
G

Greg

Helmut,

Consider this:

Sub FRinSelectionOnlyC()
Dim oRng As Range
Set oRng = Selection.Range
With oRng.Find
'.Wrap = wdFindContinue
'.Wrap = wdFindStop
.Text = "time"
.Replacement.Text = "hour"
.Execute Replace:=wdReplaceAll
End With
End Sub

Run with both .Wrap lines it stetted out. It works

Run with .Wrap wdFindContinue it fails
Run with .Wrap wdFindStop it works
Run again with wdFindContinue it fails
Run with both stetted out again it works

The quirk (if that is what we call it) appears to be in tied to the
..Wrap line.
 
J

Jay Freedman

Hi Greg,

You're right, my code is trash. I know I had it working last night, but
after I posted it I threw away the file, so I don't know what I did. :(

Anyway, you and Jean-Guy and Helmut have all come up with working versions.
For clarity of understanding -- that is, absence of "magic" -- I prefer
Jean-Guy's version.
 
G

Greg Maxey

Jay,

"Trash." That is a little harse :)

There is a eastern proverb. "Sometimes even experienced monkeys fall out of
the tree."

JGM, thanks for you suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top