Do Loop

M

Marty

I have a maco that selects each line, checks its
properties, and performs a task. I would like the macro
to run until EOF. When I put:

Do While Not EOF(1)

I get a bad file name or number error. I used the file
name by setting the file name to a variable and I get a
type mismatch error. How can I get this loop to work?

TIA, Marty
 
J

Jay Freedman

Hi Marty

You can't use EOF with a Word document. It's for files opened with the Open
<file> For Input As #1 kind of syntax. That works for a plain-text or binary
file, but it'll make hash out of a Word file.

If your "lines" are actually *paragraphs* -- if each one ends with a
paragraph mark -- you can use this kind of loop:

Dim MyPara As Paragraph

For Each MyPara In ActiveDocument.Paragraphs
' do whatever you need with MyPara.Range
' or MyPara.Range.Text
Next MyPara

This will process each paragraph in the document, from start to finish, and
automatically stop.

If you really mean lines, as in automatically word-wrapped within
paragraphs, it gets a bit harder because Word doesn't have a Lines
collection -- it recomputes lines on the fly, according to what the printer
driver tells it. You can use a construct like

Selection.Bookmarks("\line").Range

but it's clumsy. You have to make the Selection move through the document
and watch for when it won't move any further.
 

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