5
votes

I have the following part of the code which slows down in execution over time. My Word document has over 200 pages and around 12000 paragraphs. At around 3000th paragraph it can be said that execution is a few times slower than at start.

Any way I can keep the speed on the same level? Maybe iterating through paragraphs is no way to go at all for big documents?

Dim worddoc As Word.Document
Dim ParaCount as long, J as long, x as long
Dim RowData as string

With worddoc
    ParaCount = .Paragraphs.Count
    For J = 1 To ParaCount    
        RowData = .Paragraphs(J).Range.Text       
        x = x + 1
        If x Mod 10 = 0 Then Application.StatusBar = x
    Next ParaCount
End With
1
It worked, speed is on the same good level for all elements. Please post it as answer.Ryszard Jędraszyk

1 Answers

9
votes

Something like this should work. I believe you already have it working, but for posterity here is the For Each approach mentioned.

Public Sub Iterate_Paragraphs()
    Dim Paragraph As Word.Paragraph

    For Each Paragraph In ActiveDocument.Paragraphs
        Debug.Print Paragraph.Range.Text
    Next
End Sub