Word slows down while processing this very simple code snippet

F

Fool

I'm trying to do string processing on an index in a MS Word document.
The document itself is about 700 pages, and there are approximately
2500 lines in the index.

The code included here is just test code, but it demonstrates the
problem I am having. The code runs quickly until line 50 or so and
then gets slower and slower until it grinds to a halt an hour or two
later. It does not seem to matter what the initial value of 'n' is.

I'm wondering why the big slowdown performing a task that shoudn't eat
memory, and how it can be avoided. Any suggestions appreciated!

Sub testMe()

Dim r As Range
Dim n As Integer
Dim iEnd As Integer

iEnd = ActiveDocument.Indexes(1).Range.Paragraphs.Count
n = 1

While n < iEnd
Set r = ActiveDocument.Indexes(1).Range.Paragraphs(n).Range
Debug.Print "n is " & n
n = n + 1
Wend

End Sub
 
M

Malcolm Smith

I've seen this before; it seems that each iteration makes Word recount all
the paragraphs until the correct one.

I have found that:

dim oPara as Paragraph

for each oPara in ActiveDocument.Paragraphs
' Whatever
Next oPara

Is massively quicker after the first few paragraphs. I haven't tried it
with

For Each oPara in ActiveDocument.Indexes(1).Range.Paragraphs

but you could give it a go.

- Malc
www.dragondrop.com
 
F

Fool

Thanks! I will try it and post the results later.

On Sun, 23 Nov 2003 23:34 +0000 (GMT Standard Time),
 

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