Loop help, please?

E

Ed

I'm trying to write a routine that includes
Do Until
Loop
some more stuff, then
Do While
Loop
then call out and run another macro.

When the macro is done, I want to go back up to the very top and run
everything all over again until a certain criteria is met (like a Do Until -
Loop). The criteria, though, is exactly the same as the criteria for the
first Do Until. Can I write
Do Until this
Do Until this
Loop
Loop
without confusing things? Or would it be better to put each of the "inner"
loops in subroutines?

Ed
 
D

Doug Robbins - Word MVP

Hi Ed,

Better to give us some idea what "some more stuff is" as there are horses
for courses.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
E

Ed

Don't say you didn't ask! 8>/

Since I posted I was playing, trying to see what I could do, and got this.
This is run on a three-column tab-delimited text file.

Sub TestMe1()

' Created 07-01-03 by Ed Millis
' to separate query into individual Word docs
'
' Sets up the document

' Drop to the end of the document and insert a
' table cell as a marker
<DELETED CODE FOR BREVITY>
<IT WORKED WHEN I STEPPED THROUGH IT>

' Formats the document margins
With ActiveDocument.PageSetup
<DELETED CODE FOR BREVITY>
<IT WORKED WHEN I STEPPED THROUGH IT>
End With

' The first sub deletes empty lines until it hits the
' table cell at the end of the document
Application.Run MacroName:="SubRoutine1"


' Deletes the entire first line of text, the header row
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

End Sub

This is the Subroutine1:

Sub SubRoutine1()

' Written 07/01/03 by Ed Millis

' Do only if not in a table
Do While Selection.Information(wdWithInTable) = False

' Detects if the cursor is on a valid text entry or an empty line,
' and deletes an empty line
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
If Selection.Characters.Count < 2 Then
Selection.Delete
Else: Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=1
End If

' If there was not a valid entry, start over
Loop

End Sub

The SubRoutine1 begins at the top of the document and runs down to the table
cell at the end, deleting all empty lines. It works - when run by itself!
When stepping through, however, the Application.Run line is highlighted, the
top line is deleted, and the debugger drops to End Sub. I can't tell if the
top line is deleted because of the SubRoutine1 or the last set of
instructions.

The idea is that right after where I have stopped writing the main sub,
there will be more instructions that will be looped, also with the Do While
Selection.Information(wdWithInTable) = False criteria. I was hoping that,
by enclosing it in a separate macro running within this one, I could loop
without interference. Except the SubRoutine1 apparently won't loop by
itself from within this macro.

Have I successfully confused you?

Ed
 
D

Doug Robbins - Word MVP

Hi Ed,

No, I would not say that I am confused, but if the empty lines are actually
empty paragraphs ¶, you could use:

Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^13{2,}", ReplaceWith:="^p",
MatchWildcards:=True, Wrap:=wdFindContinue, Forward:=True) = True
Loop
End With

which would be whole lot quicker.

So, as I said, there are horses for courses. We still don't no anything
about the course.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 

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