使用 OLE Interop 打开 Word 文档时如何停止宏运行?

发布于 2024-11-13 12:01:49 字数 383 浏览 2 评论 0原文

正如标题所示,我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我爱人 2024-11-20 12:01:49

尝试:

WordBasic.DisableAutoMacros 1

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

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


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

Try:

WordBasic.DisableAutoMacros 1

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

久随 2024-11-20 12:01:49

对于 c#,您可以使用

(_wordApp.WordBasic asdynamic).DisableAutoMacros();

我使用的整个代码是:

using Word = Microsoft.Office.Interop.Word;
private Word.Application _wordApp;
...
                    _wordApp = new Word.Application
                {
                    Visible = false,
                    ScreenUpdating = false,
                    DisplayAlerts = Word.WdAlertLevel.wdAlertsNone,
                    FileValidation = MsoFileValidationMode.msoFileValidationSkip
                };

                _wordApp.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
                (_wordApp.WordBasic as dynamic).DisableAutoMacros();

For c# you can use

(_wordApp.WordBasic as dynamic).DisableAutoMacros();

The whole code I'm using is:

using Word = Microsoft.Office.Interop.Word;
private Word.Application _wordApp;
...
                    _wordApp = new Word.Application
                {
                    Visible = false,
                    ScreenUpdating = false,
                    DisplayAlerts = Word.WdAlertLevel.wdAlertsNone,
                    FileValidation = MsoFileValidationMode.msoFileValidationSkip
                };

                _wordApp.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
                (_wordApp.WordBasic as dynamic).DisableAutoMacros();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文