VBA 会随着时间变慢 - 我可以将对象加载到内存中以加快速度吗? [英] VBA slows over time - can I load my object into memory to speed things up?

查看:47
本文介绍了VBA 会随着时间变慢 - 我可以将对象加载到内存中以加快速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 excel 中运行 vba 来读取 Word 文档中的段落.代码工作正常,但是,我正在阅读 10000 个段落,当我读到最后时,循环正在爬行.我想尝试将单词 doc 读入内存,然后尝试循环以查看它是否加速.问题是我不知道如何做到这一点.有什么建议吗?

I'm running vba in excel to read paragraphs from a word document. The code works fine, however, I'm reading 10000 paragraphs and by the time I get to the end the loop is crawling. I wanted to try reading the word doc into memory then attempting the loop to see if it speeds up. The thing is that I'm not sure how to do this. Any suggestions?

这是我目前拥有的

Set wdDoc = GetObject(wdFileName)
Set myParas = wdDoc.Paragraphs
ParCount = myParas.Count

For X = 0 To ParCount  ' ParCount is 10,000
    With myParas(X)
        pLevel = .OutlineLevel
    End With
Next X

推荐答案

Word 不知道每个段落的位置,所以在使用 document.Paragraphs(1234) 时必须从第一段.这就是为什么 For EachFor 快得多的原因:

Word doesn't know the position of each paragraph, so when using document.Paragraphs(1234) it has to start searching from the first paragraph. That is why For Each would be much faster than For:

Dim p As Word.Paragraph

For Each p in document.Paragraphs
    ' do stuff
Next

这篇关于VBA 会随着时间变慢 - 我可以将对象加载到内存中以加快速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆