Range Object

J

Joanne

I assigned a variable oRnge=ActiveDocument.Range and then performed a search
for a string of numbers. Once the string is found, the text is manipulated
and the macro goes on to search for the next string of numbers. In other
words, the macro works fine, but the value of "oRnge" changes to the value of
the search results. My question is how can the macro continue to search
through the document when the value of oRnge has changed? I'm just curious
as to how this works.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?Sm9hbm5l?=,
I assigned a variable oRnge=ActiveDocument.Range and then performed a search
for a string of numbers. Once the string is found, the text is manipulated
and the macro goes on to search for the next string of numbers. In other
words, the macro works fine, but the value of "oRnge" changes to the value of
the search results. My question is how can the macro continue to search
through the document when the value of oRnge has changed? I'm just curious
as to how this works.
My preference is to collapse the found range to its end-point (rng.Collapse
wdCollapseEnd), then extend the End property to the End property of the
document's range (rng.End = doc.Range.End).

Note, however, that if your document contains tables, and the found range might
be in a table cell, you MAY have to work differently as extending a range beyond
the cell will include the entire table in the range. This is fine, as long as
you've altered the text in the found range so that it won't be found again.
Otherwise, you end up in a loop.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
K

Klaus Linke

And another option would be to start with an empty Range at the top, search downwards, and stop if nothing more is found...
Something like

Dim myRange As Range
Set myRange = ActiveDocument.Range(0, 0)
With myRange.Find
.Text = "a"
.Wrap = wdFindStop
.Forward = True
' other stuff as needed
End With
Do While myRange.Find.Execute
' myRange.Select
' or do whatever
myRange.Collapse(wdCollapseEnd)
Loop

Collapsing myRange to its end-point is necessary if you change the text of myRange, so Word doesn't look for your Find text in myRange, causing it to loop endlessly (if it finds it) or stopping prematurely (if it doesn't).

Regards,
Klaus
 

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