从 Excel VBA 损坏和自动化错误中恢复

Recovery from Excel VBA corruption and Automation errors

我有一个 Excel 工作簿,其中有很多内容 – 宏、外部和实时数据源等 – 上个月大约每周一次崩溃。

当 运行 一个宏并得到:

时,通常会出现损坏
Run-time error '-2147319767 (80028029)':

Automation error
Invalid forward reference, or reference to uncompiled type.

调试器识别的故障点毫无意义——相同的代码已经运行了数周。我一直在使用的修复方法是回滚到未抛出错误 运行 宏的工作簿的已保存版本,并且它始终包含完全相同的 VBA 代码。所以我得出结论,幕后的某些东西正在被破坏。

这是怎么回事?有没有办法避免这种情况?有没有比回滚到工作簿的早期保存版本更好的修复方法?

有很多 questions regarding this error, and the code changes that fix them never make sense either. The one thing they all have in common is that they make a change to the VBA code that, one deduces,迫使 Excel 重新生成其伪代码。

进一步的研究导致人们经常提到 Excel 工作簿损坏,以及一个名为 Excel VBA Code Cleaner 的免费实用程序。该实用程序的作者解释了发生了什么:

During the process of creating VBA programs a lot of junk code builds up in your files. If you don't clean your files periodically you will begin to experience strange problems caused by this extra baggage. Cleaning a project involves exporting the contents of all its VBComponents to text files, deleting the components and then importing the components back from the text files.

遗憾的是,他们尚未发布适用于 64 位 Excel 的实用程序版本。但是可以手动执行相同的操作——保存所有 VB 代码,删除所有模块,然后重新创建它们并将代码粘贴回去。

更新: VBA Code Decompiler 是另一个似乎可以完成同样事情的免费软件实用程序。还有更详细的描述,说明 Office 如何编译和持久化 VB其文件中的代码。