Returning a find within a range

P

Paul Appleby

I'm using Find on a range object with a limited range to try and avoid
searching more of the document than necessary as I'm repeating the operation
many times.

In most circumstances the result returns within the range (assuming there is
a match). However when the entire selected range matches what is being
searched for (which may include formatting and/or wildcards) then Word
returns the next available match within the document - even though it is
outside of the range being searched.

It seems to me that if the properties of the range match what is being
searched for then Word will not return the range as the result but return the
next possible match. Can someone confirm if this is what is happening? And if
so, can someone suggest a possible way to stop it from happening?

Thanks

Paul
 
G

Greg Maxey

Paul,

Yes that is the way it works. I read an pretty good explanation of how
..Find goes about redefining a search range following each .Execute, but I
can't find it now. Basically, if the found text matches the search range
text "exactly" then Word loses control of the initial defined range,
interprets a successful .find operation and then continues the .find
procedure to IAW the .Wrap setting including the rest of the document.

Here is an example of how you might work around this. Tested on a document
containing this text. One, two, three. One, two, three.


Sub Test()
Dim oRng As Word.Range
Dim oRngDup As Word.Range
Dim oDoc As Document
Set oDoc = ActiveDocument
Set oRng = oDoc.Range
oRng.Start = oDoc.Words(1).Start
oRng.End = oDoc.Words(1).End
Set oRngDup = oRng.Duplicate
With oRng.Find
.Text = oDoc.Words(1).Text
While .Execute
If oRng.End < oRngDup.End Then
oRng.Select
Else
MsgBox "Text not found."
End If
Wend
End With
End Sub
 

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