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 / March 2005

Tip: Looking for answers? Try searching our database.

Range Object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joanne - 29 Mar 2005 18:47 GMT
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.
Cindy M  -WordMVP- - 29 Mar 2005 21:50 GMT
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
[quoted text clipped - 3 lines]
> 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 :-)
Klaus Linke - 31 Mar 2005 02:12 GMT
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

"Cindy M -WordMVP-" <C.Meister-C@hispeed.ch> wrote:
> Hi =?Utf-8?B?Sm9hbm5l?=,
>
[quoted text clipped - 23 lines]
> This reply is posted in the Newsgroup; please post any follow question or reply
> in the newsgroup and not by e-mail :-)
 
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.