如何抑制 VIsual Basic“宏已禁用”消息在 C# 中打开 Word 文档时显示的对话框

发布于 2024-09-25 02:33:35 字数 1069 浏览 6 评论 0原文

我有一个非常烦人的问题。

我通过 c# / COM Interop (Word 2007) 打开一个 Word 文档。我想阻止宏运行,因为其中一些宏可能会崩溃(缺少书签的旧文件等)。

因此,我运行以下代码来抑制可能弹出的所有对话框:

Word.ApplicationClass wordApplication = new Word.ApplicationClass();

wordApplication.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

wordApplication.ShowVisualBasicEditor = false;

wordApplication.AutomationSecurity = 
  Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

Word.Document wordDoc = null;

当我执行以下代码时,我收到消息框“此项目中的宏已禁用”。

 wordDoc = wordApplication.Documents.Open(
                ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing);

我需要抑制此消息,检测它是否正在显示并“确定”它,或者找到一种禁用启动宏而不会妨碍 VB 的方法。

有什么想法吗?

I have a really annoying problem.

I am openign a word document via c# / COM Interop (Word 2007). I want to prevent the Macros from Running as some of them may crash (older files with missing bookmarks etc.).

So I run the following code to suppress all the dialogs that may pop up:

Word.ApplicationClass wordApplication = new Word.ApplicationClass();

wordApplication.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

wordApplication.ShowVisualBasicEditor = false;

wordApplication.AutomationSecurity = 
  Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

Word.Document wordDoc = null;

When I then execute the following code, I get the message box "Macros in this Project are Disabled".

 wordDoc = wordApplication.Documents.Open(
                ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing);

I need to either suppress this message, dettect if it is being shown and "ok" it or find a way of disableing startup macros without VB getting in the way.

Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风吹短裙飘 2024-10-02 02:33:35

我从来无法让它在 C# 中工作,但在 VB.NET 中却没问题。操作方法如下:如何使用 PowerShell 打开包含 AutoOpen 宏的文档?

I never could get this to work with C#, but with VB.NET it was fine. Here's how to do it: How to open document that contains AutoOpen macro with PowerShell?

荒岛晴空 2024-10-02 02:33:35

本文可能会有所帮助。我不确定你如何将其转换为 C#。

VBA 代码:

WordBasic.DisableAutoMacros 1   ' Disables auto macros
WordBasic.DisableAutoMacros 0   ' Enables auto macros

我不知道这是否会禁用“此项目中的宏已禁用”消息。

This article might help. I'm not sure how you'd translate it to C#.

VBA code:

WordBasic.DisableAutoMacros 1   ' Disables auto macros
WordBasic.DisableAutoMacros 0   ' Enables auto macros

I don't know if this will disable the "Macros in this Project are Disabled" message.

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