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 2005

Tip: Looking for answers? Try searching our database.

How to declare a end of document loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank - 24 Feb 2005 09:13 GMT
I try to make a simple macro that repeatedly search an active document and
make some editing each time the search success.

How do I make the repeat loop?

The macro looks like this:

Selection.HomeKey Unit:=wdStory
While Not <end of document>
   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "No table output."
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   Selection.TypeBackspace
   Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
   Selection.TypeBackspace
   Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
   If Selection.Text <> "" Then Selection.TypeBackspace
   Selection.TypeBackspace
 Wend
   Selection.HomeKey Unit:=wdStory
End Sub

Thanks, Frank
Jonathan West - 24 Feb 2005 09:28 GMT
>I try to make a simple macro that repeatedly search an active document and
> make some editing each time the search success.
>
> How do I make the repeat loop?
>
> The macro looks like this:

Change it to this

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
   .Text = "No table output."
   .Replacement.Text = ""
   .Forward = True
   .Wrap = wdFindStop
   .Format = False
   .MatchCase = False
   .MatchWholeWord = False
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False
   Do While .Execute
       Selection.TypeBackspace
       Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
       Selection.TypeBackspace
       Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
       If Selection.Text <> "" Then Selection.TypeBackspace
       Selection.TypeBackspace
   Wend
End With
Selection.HomeKey Unit:=wdStory
End Sub

The trick to this is that the Execute method of the Find object returns a
boolean value depending on whether it found something. You can branch
depending on the result.

I've also change the Wrap property to wdFindStop, so that it doesn't try to
start again at the beginning of the document when the search reaches the
end.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Frank - 24 Feb 2005 10:13 GMT
Thank you very much for your solution. I just had to replase the "wend"
keyword with "loop" and then it worked just perfect.

Frank

> >I try to make a simple macro that repeatedly search an active document and
> > make some editing each time the search success.
[quoted text clipped - 37 lines]
> start again at the beginning of the document when the search reaches the
> end.
 
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.