使用 OLE Interop 打开 Word 文档时如何停止宏运行?
正如标题所示,我有一个 .Net 应用程序,它使用互操作在 Word 中打开文档。 设置了
app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
我在打开文档之前 。根据文档,这“禁用以编程方式打开的所有文件中的所有宏,而不显示任何安全警报”
但是,当我尝试打开一个特定文档时,屏幕上出现一个对话框,显示“无法加载对象,因为它在本机上不可用”。这是一份客户文档,但我相信它包含一个宏,其中引用了我尚未安装的 COM 对象。
我是不是在做蠢事?有没有办法在打开 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"
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.
Am I doing something stupid? is there any way to actually disable macros when opening a Word document?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
奇怪的是,这依赖于 VBA 之前的时代,但似乎仍然是确保不触发自动宏的最可靠方法(在任何文档中 - 您可能想要使用参数“0”将其返回)。
我最近有一个项目,我必须处理 6,000 个 Word 模板(是的,模板,不是文档),其中许多模板都有宏等奇怪的东西。使用这种技术,我能够处理除 6 个之外的所有模板。 (我从来没有弄清楚这 6 个问题出在哪里)。
编辑:有关如何从 C# 调用此函数的讨论,请参阅: http:// www.dotnet247.com/247reference/msgs/56/281785.aspx
Try:
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").
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).
EDIT: for a discussion of how to call this from C#, see: http://www.dotnet247.com/247reference/msgs/56/281785.aspx
对于 c#,您可以使用
(_wordApp.WordBasic asdynamic).DisableAutoMacros();
我使用的整个代码是:
For c# you can use
(_wordApp.WordBasic as dynamic).DisableAutoMacros();
The whole code I'm using is: