【问题标题】:HOw do I suppress the VIsual Basic "Macros are Disabled" Dialog that shows when opening a word doc in C#如何抑制在 C# 中打开 word doc 时显示的 VIsual Basic“宏已禁用”对话框
【发布时间】:2011-04-16 06:49:28
【问题描述】:

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

我正在通过 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 不会妨碍它。

有什么想法吗?

【问题讨论】:

  • 只是想跟进看看以下是否回答了您的问题。

标签: c# .net ms-word automation ms-office


【解决方案1】:

This article 可能会有所帮助。我不确定你如何将它翻译成 C#。

VBA 代码:

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

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

【讨论】:

    【解决方案2】:

    我永远无法让它与 C# 一起使用,但使用 VB.NET 就可以了。操作方法如下:How to open document that contains AutoOpen macro with PowerShell?

    【讨论】:

    • 托德,距离这个答案已经有几年了。你知道有什么新东西可以在 C# 中工作吗?
    • @arbitrarystringofletters 不幸的是,没有。 Office 编程继续为 VB/VBA 更好/更容易地工作。我猜 C# 在其他任何地方都获得了所有荣耀,但在 Office 中,VB.NET 仍然占主导地位。
    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 2018-07-20
    • 2014-01-01
    • 1970-01-01
    • 2015-05-07
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多