Repeat Macro until end of file encountered

M

Marla 5670

BTW, I used Do Until True...Loop--with bad results. Two more questions:
Is there a way to say Do Until End of File?
Also, is there a keystroke to stop a repeating macro?
 
M

Marla 5670

Thanks for the tip. My original macro ran and ran, then crashed and
burned...Word shut down and the original macro was corrupted!

There must also be a way to say Do Until EOF Encountered or Do until Not
Found? I used to do these things in WordPerfect many years ago, and I need
the equivalent in Word. Something simple?
 
R

Russ

Marla,

A Google Groups search found this clip from a Jay Freedman answer to a post
in 2003.
-------------------------------------------
This example macro shows the general outline of how to search repeatedly
until nothing more is found. [Note the difference in criteria -- not "until
end of document", but "until no more finds".]

Sub FindAndFix()
Dim MyRange As Range

Set MyRange = ActiveDocument.Range

With MyRange.Find
' set parameters as needed --
' beware of "sticky" values from
' previous searches
.ClearFormatting
.Text = "target"
.Format = False
.Forward = True ' important
.Wrap = wdFindStop 'important
.MatchWildcards = False

' the .Execute method returns True when it
' finds something, False when unsuccessful
Do While .Execute
' when .Execute is successful,
' MyRange now includes only the found text

' do something to MyRange here
MyRange.Sentences(1).Case = wdTitleWord

' start next search after current find
' to avoid finding the same one again
MyRange.Collapse wdCollapseEnd
Loop
End With
End Sub
 

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