本文介绍了使用 OLE Interop 打开 Word 文档时如何停止运行宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,我有一个 .Net 应用程序,它使用互操作在 Word 中打开文档.我已经设置了

As the title suggests, I have a .Net application which uses interop to open documents in Word. I have set

app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable

在打开文档之前.根据文档,此禁用以编程方式打开的所有文件中的所有宏,而不显示任何安全警报"

before opening the document. According to the documentation, thhis "Disables all macros in all files opened programmatically, without showing any security alerts"

但是,当我尝试打开一个特定文档时,我在屏幕上看到一个对话框,显示无法加载对象,因为它在这台机器上不可用".这是一个客户文档,但我相信它包含一个宏,其中包含对我尚未安装的 COM 对象的引用.

However, when I attempt to open one specific document I get a dialog box on the screen that says "could not load an object because it is not available on this machine". It's a customer document but I believe it contains a macro with references to a COM object which I don't have installed.

我在做蠢事吗?有没有办法实际上在打开 Word 文档时禁用宏?

Am I doing something stupid? is there any way to actually disable macros when opening a Word document?

推荐答案

尝试:

WordBasic.DisableAutoMacros 1

奇怪的是,这依赖于回到 VBA 之前的时代,但似乎仍然是确保不触发自动宏的最可靠方法(在任何文档中 - 您可能想要使用参数0"将其返回).

Bizarrely, this relies on a throwback to pre-VBA days, but still seems to be the most-reliable way to ensure that no auto macros are triggered (in any document - you may want to turn it back using the parameter "0").

我最近有一个项目,我必须处理 6,000 个 Word 模板(是的,模板,而不是文档),其中许多模板包含宏等奇怪的东西.使用这种技术,我能够处理除 6 个之外的所有模板.(我从来没有弄清楚那 6 个有什么问题).

I recently had a project where I had to process 6,000 Word templates (yes, templates, not documents) many of which had oddball stuff like macros, etc. I was able to process all but 6 using this technique. (I never did figure out what the problem was with those 6).

有关如何从 C# 调用它的讨论,请参阅:http://www.dotnet247.com/247reference/msgs/56/281785.aspx

for a discussion of how to call this from C#, see: http://www.dotnet247.com/247reference/msgs/56/281785.aspx

这篇关于使用 OLE Interop 打开 Word 文档时如何停止运行宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:48
查看更多