Looping A Macro Help!

S

sbs

I need help with this one!

I want to loop a macro based upon how many lines are in the curren
word document. In the document properties under the statistics tab th
number of lines is listed. How do you take that value and loop a
existing macro based upon that number. I appreciate anyone taking hi
or her time in helping me.

Scot
 
H

Helmut Weber

Hi Scott,
how about this one:
Sub Test601()
Sub Test601()
Dim l As Integer ' number of lines
Dim i As Integer ' just a counter
l = ActiveDocument.ComputeStatistics(wdStatisticLines)
With Selection
.WholeStory
.ExtendMode = False
.Collapse
For i = 1 To l
.GoTo what:=wdGoToLine, which:=wdGoToAbsolute, Count:=i
.Bookmarks("\line").Select
MsgBox "line " & i
Next
End With
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jay Freedman

I need help with this one!
I want to loop a macro based upon how many lines are in the current
word document. In the document properties under the statistics tab the
number of lines is listed. How do you take that value and loop an
existing macro based upon that number. I appreciate anyone taking his
or her time in helping me.

Scott

Hi Scott,

It's easier in VBA to get the number from the Tools > Word Count dialog than
from the document statistics dialog. Try this....

Dim DocLines As Long
With Dialogs(wdDialogToolsWordCount)
.Execute
DocLines = .Lines
End With

For i = 1 To DocLines
' do something
Next i

In general, though, Word doesn't like doing "line-by-line" manipulations.
Lines are dynamically changing, impermanent things in Word, constantly
recalculated based on the preceding text, formatting, the current printer
driver, etc. If what you're calling "lines" are really one-line paragraphs,
it's much better to base the macro on working through the paragraphs.

Often you can avoid dealing with lines and paragraphs altogether by using
the .Find method of a Range object -- that will be both easier to program
and faster to run.
 

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