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

Tip: Looking for answers? Try searching our database.

Stopping a Do While Loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Beamer - 29 Sep 2005 19:15 GMT
I have programming setup to run inside a Do While loop where it looks for
certain text and then deletes the row.  The problem I am having is getting
the loop to stop when it has found all of text in the document.  My current
coding is:

Do While XAB < 40
           .Forward = True
           .Wrap = wdFindContinue
           .MatchWholeWord = True
           .MatchAllWordForms = False
           .Execute FindText:="%<>%"
           Selection.Rows.Delete
           XAB = XAB + 1
Loop

Any suggestions would be appreciated.
Anand.V.V.N - 29 Sep 2005 19:31 GMT
Instead of
XAB = XAB + 1
use this
if xab=40
exit do
else
XAB=XAB+1
endif

Exit do whould exit the do loop.

Hope   this was useful.

Anand
Signature

"Who will guard the guards?"

> I have programming setup to run inside a Do While loop where it looks for
> certain text and then deletes the row.  The problem I am having is getting
[quoted text clipped - 12 lines]
>
> Any suggestions would be appreciated.
Beamer - 29 Sep 2005 19:49 GMT
The thing is I would actually like to find a conditional statement that would
tell the loop to exit when there were no more of the strings that it is
looking for in the document.  The current use of a count to 40 is only
because that is the maximum number of times the loop would need to run,
however I would like for the programming to be more efficient and only loop
as many times as necessary.

> Instead of
> XAB = XAB + 1
[quoted text clipped - 27 lines]
> >
> > Any suggestions would be appreciated.
Tony Jollans - 29 Sep 2005 20:16 GMT
Most of the code inside your loop can be outside it - the settings only need
doing once, so you could have

.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
MatchAllWordForms = False

Do While XAB < 40
   .Execute FindText:="%<>%"
   Selection.Rows.Delete
   XAB = XAB + 1
Loop

That makes it easier to see what's going on. Now, what you want is to check
if  the execute has been successful. As it returns a True/False value, you
can do this

Do While XAB < 40
   If .Execute FindText:="%<>%" Then
       Selection.Rows.Delete
   Else
       Exit Do
   End If
   XAB = XAB + 1
Loop

Now you can see that the XAB variable is unnecessary ...

Do
   If .Execute FindText:="%<>%" Then
       Selection.Rows.Delete
   Else
       Exit Do
   End If
Loop

which can be shortened to:

Do While .Execute FindText:="%<>%"
   Selection.Rows.Delete
Loop

Giving complete code of:

.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
MatchAllWordForms = False
Do While .Execute FindText:="%<>%"
   Selection.Rows.Delete
Loop

Enjoy,
Tony

> The thing is I would actually like to find a conditional statement that would
> tell the loop to exit when there were no more of the strings that it is
[quoted text clipped - 36 lines]
> > >
> > > Any suggestions would be appreciated.
Beamer - 29 Sep 2005 20:56 GMT
The first tip that you gave will reduce the lines run, however I ran into a
problem where the If statement ( If .Execute FindText:="%<>%" Then) throws an
error because of an error in the conditional statement.  I believe that the
error directly relates to the space between .Execute and FindText.  Is there
a way to correct this error using brackets, or do I need to use an entirely
different conditional statement?

-Beamer

> Most of the code inside your loop can be outside it - the settings only need
> doing once, so you could have
[quoted text clipped - 97 lines]
> > > >
> > > > Any suggestions would be appreciated.
Tony Jollans - 29 Sep 2005 21:36 GMT
My apologies - I should test instead of just typing :)

The correct syntax is:

If .Execute(FindText:="%<>%") Then

and, in the While:

Do While .Execute(FindText:="%<>%")

Enjoy,
Tony

> The first tip that you gave will reduce the lines run, however I ran into a
> problem where the If statement ( If .Execute FindText:="%<>%" Then) throws an
[quoted text clipped - 106 lines]
> > > > >
> > > > > Any suggestions would be appreciated.

Rate this thread:






 
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.