简体   繁体   English

VBA-Word-遍历段落的速度会随着时间的推移而显着降低

[英]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. 我的Word文档有200多页和大约12000个段落。 At around 3000th paragraph it can be said that execution is a few times slower than at start. 在第3000段左右,可以说执行速度比开始时慢了几倍。

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. 我相信您已经可以使用它了,但是为了后代,这里提到了“ For Each方法。

Public Sub Iterate_Paragraphs()
    Dim Paragraph As Word.Paragraph

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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