How to search for multiple items within a range....?

F

Fool

I know this has been asked before, but my Google search only turned up
threads that didn't satisfactorily answer the question (at least for
me)...

I want to find as many instances of XYZ as possible within a range
(Range1) of a document. I set up a Find operation using Range1.Find,
and add all hits to a collection.

The problem is that Range1.Find works by resetting Range1 to whatever
it finds. It reliably finds every instance of XYZ starting with the
start of the original Range1, and going to the very end of the
document... in other words, it doesn't stop when I want it to, as the
original parameters of Range1 were lost with the first hit.

(I personally think this is an awkward way of implementing Search...)

OK, so what is the simplest / most elegant / most universally accepted
way of forcing Word to only return hits from within that initial
range?

TIA!

Fool
 
J

Jonathan West

Hi Fool,

You are nearly there already. There are three things that you need to do.

1. Define a Range object variable that covers are area you want to search
2. Set Wrap property of the Find object to wdFindStop. This will stop
repeated finds from going beyond the chosen range.
3. After each use of the Execute method, you must not in any way modify the
range variable, or the find gets reset and the next find forgets about the
end of the original range.
 
F

Fool

Thanks for the reply, but I must be missing something. Here's my
current code:
________________________

counter = 0

While counter < 100

With Range1.Find
.ClearFormatting
.MatchWildcards = True
.Forward = True
.Text = StringToLookFor
.Wrap = wdFindStop
.Execute

If .Found = False Then goto LeaveThisLoop

colParagraphNumbers.Add Range1
End With

counter = counter + 1
Wend

_________________________


From what I understand, this loop should find each instance of
StringToLookFor within Range1, and then leave the loop when no more
instances are found (if .found = false....)

What actually happens is, it runs past Range1's original end, and will
always get a full 100 hits (from the rest of the document) before
quitting. I don't believe I'm modifying Range1 in any way by adding it
to a collection after each successful hit... any suggestions on what
I'm doing wrong?

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