Find.Text found but Replacement not exectuted.

G

Greg Maxey

I type the word "One" in a document, select it and bookmark it as "BM"

I run this code:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceOne
If .Found Then
oRng.Select
MsgBox "Item found"
End If
End With
End Sub

The oRng is selected, the message displays, but the replacement does
not occur.

If I add a space before or after the word "One" and include it in the
bookmarked range then the replacement does occur.

Can anyone explain why the replacement does not occur if the found text
includes the entire bookmarked range?

Thanks
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
I type the word "One" in a document, select it and bookmark it as "BM"

I run this code:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceOne
If .Found Then
oRng.Select
MsgBox "Item found"
End If
End With
End Sub

The oRng is selected, the message displays, but the replacement does
not occur.

If I add a space before or after the word "One" and include it in the
bookmarked range then the replacement does occur.

Can anyone explain why the replacement does not occur if the found
text includes the entire bookmarked range?

Change
wdReplaceOne
by
wdReplaceAll

Maybe that if the whole range is being replaced in one operation, it does
not meet the requirement of "wdReplaceOne" as it is more of a wdReplaceWhole
kinda thing?

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

Greg Maxey

JGM,

Yes, but that wipes the bookmark :-(

Jean-Guy Marcil said:
Greg Maxey was telling us:
Greg Maxey nous racontait que :


Change
wdReplaceOne
by
wdReplaceAll

Maybe that if the whole range is being replaced in one operation, it does
not meet the requirement of "wdReplaceOne" as it is more of a wdReplaceWhole
kinda thing?

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

Jean-Guy Marcil

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

Yes, but that wipes the bookmark :-(

Of course, whenever you replace an entire bookmarked range, you lose the
bookmark.

But since you doing this by code, it is easy enough to recreate it.

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

Greg Maxey

JGM,

Well not as easily as I would have thought ;-)

Sub Scratchmacro()
Dim oRng As Word.Range
Dim tmpRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
Set tmpRng = oRng.Duplicate
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Bookmarks.Add "BM", tmpRng
End Sub
 
H

Helmut Weber

Hi everybody,

have you tried a doc that contains "one" more than once,
like:

[One]¶


One¶
Sub Scratchmacro()
Dim oRng As Word.Range
Dim tmpRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
Set tmpRng = oRng.Duplicate
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Bookmarks.Add "BM", tmpRng
End Sub

replaces both occurences, here and now,
while the bookmark is restored at the first occurence.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Helmut,

No I hadn't noticed that but you are right. Once again this illustrates
that despite hours of frustrated study I still don't fully understand the
range/selection concept. I can type the two words:

One

One

without any bookmark.

Then run this code:

Sub Scratchmacro2()
Dim oRng As Word.Range
'Select the first word "One"
Set oRng = Selection.Range
MsgBox oRng 'Confirms the intended seach range consists of the text "One"
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

And both words above are replaced with Two.

Now I can type

One One

One

And select the One One and run the same code and the result is:

Two Two

One

The same thing happens using the Selection

Sub Scratchmacro3()
With Selection.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

It appears that whenever the found range = the search range then the
wdReplaceAll method overrides the definition of the search range boundaries
and expands it to include the entire storyrange.

I am baffled with a headache.








--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Helmut said:
Hi everybody,

have you tried a doc that contains "one" more than once,
like:

[One]¶


One¶
Sub Scratchmacro()
Dim oRng As Word.Range
Dim tmpRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
Set tmpRng = oRng.Duplicate
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Bookmarks.Add "BM", tmpRng
End Sub

replaces both occurences, here and now,
while the bookmark is restored at the first occurence.
 
T

Tony Jollans

The behaviour of Find when the selection is exactly equal to the only
occurrence of the Find text varies according to version. I presume that you
are seeing the odd behaviour in 2000 - and you can see it in the UI without
using code at all. What I didn't realise was that .Found would return True.

--
Enjoy,
Tony


Greg Maxey said:
Helmut,

No I hadn't noticed that but you are right. Once again this illustrates
that despite hours of frustrated study I still don't fully understand the
range/selection concept. I can type the two words:

One

One

without any bookmark.

Then run this code:

Sub Scratchmacro2()
Dim oRng As Word.Range
'Select the first word "One"
Set oRng = Selection.Range
MsgBox oRng 'Confirms the intended seach range consists of the text "One"
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

And both words above are replaced with Two.

Now I can type

One One

One

And select the One One and run the same code and the result is:

Two Two

One

The same thing happens using the Selection

Sub Scratchmacro3()
With Selection.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

It appears that whenever the found range = the search range then the
wdReplaceAll method overrides the definition of the search range boundaries
and expands it to include the entire storyrange.

I am baffled with a headache.








--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Helmut said:
Hi everybody,

have you tried a doc that contains "one" more than once,
like:

[One]¶


One¶
Sub Scratchmacro()
Dim oRng As Word.Range
Dim tmpRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
Set tmpRng = oRng.Duplicate
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Bookmarks.Add "BM", tmpRng
End Sub

replaces both occurences, here and now,
while the bookmark is restored at the first occurence.
 
G

Greg Maxey

Tony,

I am seeing this behaviour in Word 2003.

Tony said:
The behaviour of Find when the selection is exactly equal to the only
occurrence of the Find text varies according to version. I presume that you
are seeing the odd behaviour in 2000 - and you can see it in the UI without
using code at all. What I didn't realise was that .Found would return True.

--
Enjoy,
Tony


Greg Maxey said:
Helmut,

No I hadn't noticed that but you are right. Once again this illustrates
that despite hours of frustrated study I still don't fully understand the
range/selection concept. I can type the two words:

One

One

without any bookmark.

Then run this code:

Sub Scratchmacro2()
Dim oRng As Word.Range
'Select the first word "One"
Set oRng = Selection.Range
MsgBox oRng 'Confirms the intended seach range consists of the text "One"
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

And both words above are replaced with Two.

Now I can type

One One

One

And select the One One and run the same code and the result is:

Two Two

One

The same thing happens using the Selection

Sub Scratchmacro3()
With Selection.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

It appears that whenever the found range = the search range then the
wdReplaceAll method overrides the definition of the search range boundaries
and expands it to include the entire storyrange.

I am baffled with a headache.








--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Helmut said:
Hi everybody,

have you tried a doc that contains "one" more than once,
like:

[One]¶


One¶

Sub Scratchmacro()
Dim oRng As Word.Range
Dim tmpRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
Set tmpRng = oRng.Duplicate
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Bookmarks.Add "BM", tmpRng
End Sub

replaces both occurences, here and now,
while the bookmark is restored at the first occurence.
 
T

Tony Jollans

So they've changed the UI (correctly I think) and VBA in different ways -
just to keep us on our toes presumably.

When the Range, or Selection, is not exactly equal to the search string,
what happens - as you might reasonably expect - is that searching starts at
the beginning of the range (assuming it's looking forward) and all goes,
more or less, to plan.

But, when the Range *does* exactly match the search string, F&R assumes this
is as a result of an earlier find and starts searching from the end of the
Range, and here it all goes horribly wrong. By starting outside the Range,
the range which is searched is effectively redefined as being from the end
of the original range to the end of the document (depending on the value of
..Wrap). Put another occurrence of "One" somewhere after your bookmark to see
the effect of this.

Clearly one to watch. Thank you Greg.

--
Enjoy,
Tony

Tony,

I am seeing this behaviour in Word 2003.

Tony said:
The behaviour of Find when the selection is exactly equal to the only
occurrence of the Find text varies according to version. I presume that you
are seeing the odd behaviour in 2000 - and you can see it in the UI without
using code at all. What I didn't realise was that .Found would return True.

--
Enjoy,
Tony


Greg Maxey said:
Helmut,

No I hadn't noticed that but you are right. Once again this illustrates
that despite hours of frustrated study I still don't fully understand the
range/selection concept. I can type the two words:

One

One

without any bookmark.

Then run this code:

Sub Scratchmacro2()
Dim oRng As Word.Range
'Select the first word "One"
Set oRng = Selection.Range
MsgBox oRng 'Confirms the intended seach range consists of the text "One"
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

And both words above are replaced with Two.

Now I can type

One One

One

And select the One One and run the same code and the result is:

Two Two

One

The same thing happens using the Selection

Sub Scratchmacro3()
With Selection.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
End Sub

It appears that whenever the found range = the search range then the
wdReplaceAll method overrides the definition of the search range boundaries
and expands it to include the entire storyrange.

I am baffled with a headache.








--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Helmut said:
Hi everybody,

have you tried a doc that contains "one" more than once,
like:

[One]¶


One¶

Sub Scratchmacro()
Dim oRng As Word.Range
Dim tmpRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("BM").Range
Set tmpRng = oRng.Duplicate
With oRng.Find
.Text = "One"
.Replacement.Text = "Two"
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Bookmarks.Add "BM", tmpRng
End Sub

replaces both occurences, here and now,
while the bookmark is restored at the first occurence.
 

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