Macro Speed for large indexes of ActiveDocument.Paragraphs

S

SirCodesALot

Hi,

I have a macro that is doing some indenting. Depending on the
previous paragraphs text, I may or may not have to indent all that
paragraphs children.

The problem am having is that my document has about 5000 paragraphs,
and as my index into the ActiveDocument.Paragraphs(Index) gets above
about 300, this really start slowing down... any one know why?

Here is an example of the code:

AllParagraphs = ActiveDocument.Paragraphs.Count

For i = 1 To AllParagraphs

If (ActiveDocument.Paragraphs(i).Style = sReqStyle) Then

For j = i + 1 To AllParagraphs
If (ActiveDocument.Paragraphs(j).Style =
sReqStyle) Then
If (IsSubRequirement(i, j)) Then
' indent
j = IndentRequirement(i, j) - 1
End If
End If
Next
End If
Next

As i gets above about 300, it really shows down. Is their a better
way to do this?
I have already tried turning of page updating.

thanks for your help,
-SJ
 
T

Tony Jollans

When youi reference a paragraph using ...

(Container).Paragraphs(i)

Word has to start at the beginning and count through the paragraphs until it
gets to the i'th one. The higher i, the longer it takes.

What you are doing *is* a lot of work (checking 25 million paragraphs) and I
don't know enough of your circumstances to suggest how you might reduce that
amount but you may find it a little better if you change the way of
referencing the paragraphs ...

For Each Pi in ActiveDocument.Paragraphs
If Pi.Range.End < ActiveDocument.Range.End Then
For Each Pj In ActiveDocument.Range(Pi.Range.End,
ActiveDocument.Range.End).Paragraphs
' Do your stuff ...
Next Pj
End If
Next Pi

(untested)
 
S

SirCodesALot

When youi reference a paragraph using ...

(Container).Paragraphs(i)

Word has to start at the beginning and count through the paragraphs until it
gets to the i'th one. The higher i, the longer it takes.

What you are doing *is* a lot of work (checking 25 million paragraphs) and I
don't know enough of your circumstances to suggest how you might reduce that
amount but you may find it a little better if you change the way of
referencing the paragraphs ...

For Each Pi in ActiveDocument.Paragraphs
If Pi.Range.End < ActiveDocument.Range.End Then
For Each Pj In ActiveDocument.Range(Pi.Range.End,
ActiveDocument.Range.End).Paragraphs
' Do your stuff ...
Next Pj
End If
Next Pi

(untested)

--
Enjoy,
Tony















- Show quoted text -

Tony,

Thank you for your response, I didnt know that it did that. I
appreciate you example and that should help me speed it up, i will
give it a shot!

thanks again,
-SJ
 

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