简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM