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.

Find .execute still not clicking

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 28 Mar 2005 15:57 GMT
Everytime I think that I am beginning to grasp the Find.Execute process
something comes up to cloud my understanding.

Take this small test as an example.

Document text:

Abc abc abc abc

The code:

Public Sub Test()
Dim myRange As Range

Set myRange = ActiveDocument.Range
With myRange.Find
 .ClearFormatting
 .Text = "abc"
 .Format = False
 .Forward = True
 .Wrap = wdFindStop
 .MatchCase = False
 Do While .Execute
   myRange.Font.Bold = False
   'myRange.Collapse Direction:=wdCollapseEnd
 Loop
End With
End Sub

I understand that I could use a replace:=wdReplaceAll operation here
but I am trying to get my head around stepping through each found term
and performing an action.

Most of the time when I try something like this I can spend 15 minutes
or more trying stop circular loops.  After a while it dawns on me to
put in the wdFindStop line and the Collapse Direction line.

What I don't understand is why a loop is not occurring when I run the
code above with the .collapse line activated?
Greg - 28 Mar 2005 16:19 GMT
Sorry, I meant with the .collapse line deactivate.  The macro performs
just as well with or without that line.  It seems like it should hang
up and loop on the first term found.
Klaus Linke - 28 Mar 2005 16:47 GMT
Hi Greg,

To debug, generously sprinkle some "myRange.Select" lines into your code, then single-step through it and watch what happens in the document.

If the selected text matches the .Find.Text, Word will go to the next match. You can check that in the user interface.
Still there are lots of situations where you can end up with an endless loop by accident.

Greetings,
Klaus

> Sorry, I meant with the .collapse line deactivate.  The macro performs
> just as well with or without that line.  It seems like it should hang
> up and loop on the first term found.
Helmut Weber - 28 Mar 2005 17:58 GMT
Hi Greg,

unfortunately I can't produce an endless loop,
if I want to create one.
Besides that, how the range behaves,
depends on what you do to it.

>  Do While .Execute
>    myRange.Font.Bold = False ' !!!
>    'myRange.Collapse Direction:=wdCollapseEnd
>  Loop

needs no collapsing, as the content of the range,
the string, doesn't change.

Whereas  

>  Do While .Execute
>    myRange.Font.Bold = False
    myrange.text = "xxx"  ' !!!  Content of range changes.
>    'myRange.Collapse Direction:=wdCollapseEnd
>  Loop

would need the collapsing.

The first example processes all occurences of "abc".
The second example stops after the first occurence.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Greg - 28 Mar 2005 20:30 GMT
Klaus/Helmut,

My German friends are being most helpful today.  Thank you.
 
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.