Finding Text withing a Defined Bookmark Range

G

Greg Maxey

I am seeing some weird behavior attempting to find text using a bookmark as
the defined range.

To illustrate, I created a new blank document and typed the following:

Test Test Test Test Test

I bookmarked the first "Test" as "A", the third "Test" as "B", and the last
"Test" as "C"

I then run this code:

Sub Test()
Dim oRng As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Bookmarks("A").Range
With oRng.Find
.Text = "Test"
While .Execute
i = i + 1
Wend
End With
MsgBox "Found " & i & " times."
i = 0
Set oRng = ActiveDocument.Bookmarks("B").Range
With oRng.Find
.Text = "Test"
While .Execute
i = i + 1
Wend
End With
MsgBox "Found " & i & " times."
i = 0
Set oRng = ActiveDocument.Bookmarks("C").Range
With oRng.Find
.Text = "Test"
While .Execute
MsgBox "Found"
Wend
End With
MsgBox "Found " & i & " times."
End Sub

I would expect the text to be found "1" time within each bookmark. The
results I get are 4, 2 and 0.

Can anyone explain what is going on? Thanks.
 
G

Greg Maxey

Oops. Sorry. The code is:

Sub Test()
Dim oRng As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Bookmarks("A").Range
With oRng.Find
.Text = "Test"
While .Execute
i = i + 1
Wend
End With
MsgBox "Found " & i & " times."
i = 0
Set oRng = ActiveDocument.Bookmarks("B").Range
With oRng.Find
.Text = "Test"
While .Execute
i = i + 1
Wend
End With
MsgBox "Found " & i & " times."
i = 0
Set oRng = ActiveDocument.Bookmarks("C").Range
With oRng.Find
.Text = "Test"
While .Execute
i = 1 + 1
Wend
End With
MsgBox "Found " & i & " times."
End Sub
 
J

Jay Freedman

Hi Greg,

I don't pretend to understand the "why" of this behavior, but I can tell you
how to force the code to give you the results you expect. ;-)

If you stick in the line oRng.Select after each While .Execute, and then
single-step the code with F8, you'll see that the first "hit" in each loop
is the occurrence _after_ the bookmark. That's the behavior I don't really
understand. To make it hit the occurrence _within_ the bookmark, you have to
start by collapsing the range to its beginning -- after each Set statement,
add the line oRng.Collapse wdCollapseStart.

After that, the .Execute hits each occurrence until the end of the document;
I know about that, and the solution is to use the InRange method to check
whether oRng is still inside the desired bookmark.

This version of the macro finds only the single occurrence within each
bookmark:

Sub Test()
Dim oRng As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Bookmarks("A").Range
oRng.Collapse wdCollapseStart
With oRng.Find
.Text = "Test"
While .Execute And oRng.InRange(ActiveDocument.Bookmarks("A").Range)
' oRng.Select
i = i + 1
Wend
End With
MsgBox "A Found " & i & " times."
i = 0
Set oRng = ActiveDocument.Bookmarks("B").Range
oRng.Collapse wdCollapseStart
With oRng.Find
.Text = "Test"
While .Execute And oRng.InRange(ActiveDocument.Bookmarks("B").Range)
' oRng.Select
i = i + 1
Wend
End With
MsgBox "B Found " & i & " times."
i = 0
Set oRng = ActiveDocument.Bookmarks("C").Range
oRng.Collapse wdCollapseStart
With oRng.Find
.Text = "Test"
While .Execute And oRng.InRange(ActiveDocument.Bookmarks("C").Range)
' oRng.Select
i = i + 1
Wend
End With
MsgBox "C Found " & i & " times."
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
T

Tony Jollans

Can anyone explain what is going on?

IMO, it's a bug. Word forgets the original Range.

Just to add a little to Jay's explanation (the bit he says he doesn't
understand, although I'm sure he does really) - when the Range being
searched exactly matches the Find.text, Word effectively assumes that it is
the result of a previous Find and so starts looking immediately after it.
 
G

Greg Maxey

Jay/Tony,

Thanks for the replies. I have been away all day (doing regular work), but
pondering this issue and was planning to try some things like this. Thanes
again.
 
G

Greg Maxey

Tony,

Another twist:
Just to add a little to Jay's explanation (the bit he says he doesn't
understand, although I'm sure he does really) - when the Range being
searched exactly matches the Find.text, Word effectively assumes that it
is the result of a previous Find and so starts looking immediately
after it.

If I type "Test" again at the end of the string so that it now reads:

Test Test Test Test Test Test

and the first, third, and fifth instance is bookmarked A, B, and C
respectively

Then the results returned is 5, 3, and 2!

It seems that in the last instance Word does not assume that it is the
result of the a previous find!

Buggy indeed. Thanks.
 

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