Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA - Word - iterating through paragraphs slows down incredibly over time

Tags:

ms-word

vba

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
like image 684
Ryszard Jędraszyk Avatar asked Sep 26 '16 19:09

Ryszard Jędraszyk


1 Answers

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
like image 121
Ryan Wildry Avatar answered Sep 23 '22 21:09

Ryan Wildry