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