Restarting Do ... Loop

G

G Teachman

Hello,

I'm scanning a document for the existence of the character string "| Not
rated". Then copying the selected area of text into another document. I use
a Do While loop. If "| Not rated" is found then do stuff. What I can't get
it to do is if it doesn't find "| Not rated" skip the rest of the stuff and
continue in the loop.

Here is the code in question:

Do Until <stuff>
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=4, Extend:=wdExtend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "| Not rated"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
<more stuff>
Loop

So if the Execute statement is TRUE then the right stuff continues. Not a
problem, this works. However, if the Execute statement is FALSE I want the
system to return to the beginning of the LOOP.

I can make it exit the loop by using 'Exit Do' command. However, this isn't
what I want it to do. I want it to go to the top of the DO WHILE command and
continue.

Any help would be very much appreciated.

Thanks,
 
F

Fumei2 via OfficeKB.com

I do not follow.

"if it doesn't find "| Not rated" skip the rest of the stuff and continue in
the loop."

If it does not find the search string - | Not rated - what is there to
continue to do? You are searching for one string. If the string is not
found, there is nothing else to do.

BTW: it is better to use Range, rather than Selection.

For example:

Sub SearchMe()
Dim r As Range
Dim ThisDoc As Document
Dim ThatDoc As Document

Set ThisDoc = ActiveDocument
Set r = ThisDoc.Range
Set ThatDoc = Documents.Add

With r.Find
Do While .Execute(Findtext:="| Not rated", Forward:=True) _
= True
ThatDoc.Range.InsertAfter r.Text & vbCrLf
Loop
End With
End Sub

The above searches for “|Not ratedâ€, and puts all instances of it into a new
document. Although, I am not sure what would be the purpose of that. If you
had:

Blah blah | Not rated.
Whatever | Not rated.
Yadda yadda | Not rated.

You would end up (in the new document):

| Not rated.
| Not rated.
| Not rated.

In any event, please clarify what you mean to happen if | Not rated. is not
found.
 

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