Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Returning a find within a range

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Appleby - 22 Feb 2007 22:35 GMT
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
Greg Maxey - 23 Feb 2007 00:16 GMT
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

Signature

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

> 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
[quoted text clipped - 18 lines]
>
> Paul
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.