How to declare a end of document loop

F

Frank

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
 
J

Jonathan West

Frank said:
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.
 
F

Frank

Thank you very much for your solution. I just had to replase the "wend"
keyword with "loop" and then it worked just perfect.

Frank
 

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