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.
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.