如何在使用 Microsoft.Office.Interop 打开文件时禁用弹出窗口

发布于 2024-10-30 10:58:22 字数 38 浏览 0 评论 0原文

如只读确认、其他提醒等。 如何处理这些弹出窗口?还是忽略他们?

Such as read-only confirm, other alerts.
What to do with these popups? Or ignore them?

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

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

发布评论

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

评论(6

乜一 2024-11-06 10:58:22

请参阅我的答案此处。

基本上,您可以通过“显示警报”方法禁用所有警报:

Microsoft.Office.Interop.[OFFICE_APP].Application app = new Microsoft.Office.Interop.[OFFICE_APP].Application();
app.DisplayAlerts = false;

其中 [OFFICE_APP] 是您正在使用的 Office 程序的名称,例如 Word、Excel 等。

See my answer here.

Basically, you disable all alerts via the "Display Alerts" method:

Microsoft.Office.Interop.[OFFICE_APP].Application app = new Microsoft.Office.Interop.[OFFICE_APP].Application();
app.DisplayAlerts = false;

where [OFFICE_APP] is the name of the Office program you're using, such as Word, Excel, etc.

堇色安年 2024-11-06 10:58:22

这是防止安全消息要求您允许宏的另一种选择。

我读了 这篇文章来自MSDN并找出了以下代码:

Application wordApp = new Application()
{
    Visible = false,
    AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
};

由于在打开文件之前已创建文件的副本,因此我不必将AutomationSecurity更改回默认设置。

Here is another alternative to prevent the Security message asking you to allow macros.

I read this article from MSDN and figured out the following code:

Application wordApp = new Application()
{
    Visible = false,
    AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
};

Since a copy of the file is made before opening it I don't have to change the AutomationSecurity back to the default setting.

清秋悲枫 2024-11-06 10:58:22

添加注释:对于某些受密码保护的文件格式(我测试了 .XLS,但可能还有其他格式),app.DisplayAlerts = false 将不会绕过密码对话框。

在这种情况下,您可以简单地在打开时传递一个假密码,这将引发错误。如果你愿意的话就抓住它吧。

var app = new Application();
app.DisplayAlerts = false;
var workbook = app.Workbooks.Open(filePath, "fakePassword"); // Bypasses dialog, throws error

在这种情况下抛出的错误是:

System.Runtime.InteropServices.COMException:您提供的密码
是不正确的。确认 CAPS LOCK 键已关闭,并确保
使用正确的大小写。

Adding a note: for some file formats (I tested .XLS, but probably others too) that are password protected, app.DisplayAlerts = false will NOT bypass the password dialog.

In this situation, you can simply pass a fake password on open, which will throw an error. Catch it if you want.

var app = new Application();
app.DisplayAlerts = false;
var workbook = app.Workbooks.Open(filePath, "fakePassword"); // Bypasses dialog, throws error

In this situation the error thrown is:

System.Runtime.InteropServices.COMException: The password you supplied
is not correct. Verify that the CAPS LOCK key is off and be sure to
use the correct capitalization.

飘过的浮云 2024-11-06 10:58:22

亲爱的“Uriel Fernandez”和他的想法
https: //learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.automationsecurity?view=excel-pia

让我想到了另一个想法https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.screenupdating?view=excel-pia#Microsoft_Office_Interop_Excel__Application_ScreenUpdating

所以可以尝试禁用它< strong>_Application.ScreenUpdating 和...“您将无法看到代码在做什么,但它会运行得更快”

编辑

Word.Application app = null;
try
{
app = new Word.Application();
app.Visible = false;
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
app.ScreenUpdating = false;
// work ...
}
catch {}
app.Quit();
if (app != null) Marshal.ReleaseComObject(app);

Dear "Uriel Fernandez" with his thought
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.automationsecurity?view=excel-pia

directed me to another thought https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.screenupdating?view=excel-pia#Microsoft_Office_Interop_Excel__Application_ScreenUpdating

So can try disabling it _Application.ScreenUpdating and ... "You won't be able to see what the code is doing, but it will run faster"

EDIT

Word.Application app = null;
try
{
app = new Word.Application();
app.Visible = false;
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
app.ScreenUpdating = false;
// work ...
}
catch {}
app.Quit();
if (app != null) Marshal.ReleaseComObject(app);
温折酒 2024-11-06 10:58:22

试试这个:

Microsoft.Office.Interop.Word.Application appWord = new 
Microsoft.Office.Interop.Word.Application();

appWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

这将禁用弹出窗口。

Try this:

Microsoft.Office.Interop.Word.Application appWord = new 
Microsoft.Office.Interop.Word.Application();

appWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

This will disable the popups.

心如狂蝶 2024-11-06 10:58:22

对于那些被诅咒得仍然需要使用 Excel Interop 的人来说。

可能有效的方法是使用 NetOffice.Excel nuget 包并设置以下内容:

            xlApp.Visible = false;
            xlApp.ScreenUpdating = false;
            xlApp.DisplayAlerts = false;
            xlApp.AutomationSecurity = NetOffice.OfficeApi.Enums.MsoAutomationSecurity.msoAutomationSecurityLow;
            xlApp.AskToUpdateLinks = false;
            xlWorkbook.UpdateLinks = NetOffice.ExcelApi.Enums.XlUpdateLinks.xlUpdateLinksAlways;

如果您使用的是 excel 365,您仍然会收到隐私级别警告,并记住 DisplayAlerts = false; 不会禁用安全/隐私警告。

要停止 365 上的隐私级别警告,您需要禁用电源查询上的隐私防火墙:

  1. 打开 Excel 工作簿。
  2. 从功能区中,选择数据 >获取数据> 查询选项
  3. 在左侧的“全局”下,选择隐私
  4. 选择始终忽略隐私级别设置

通过代码将“AutomationSecurity”设置为低应该可​​以做到这一点,但它对我不起作用。

对于有关外部数据的其他警告,您可以尝试在服务器的 Excel 配置上自行禁用它:

https://support.microsoft.com/en-gb/office/enable -或禁用安全警报关于来自可疑网站的链接和文件-a1ac6ae9-5c4a-4eb3-b3f8-143336039bbe

For anyone cursed enough to still have to work with Excel Interop.

What MIGHT work is using the NetOffice.Excel nuget package and setting the following:

            xlApp.Visible = false;
            xlApp.ScreenUpdating = false;
            xlApp.DisplayAlerts = false;
            xlApp.AutomationSecurity = NetOffice.OfficeApi.Enums.MsoAutomationSecurity.msoAutomationSecurityLow;
            xlApp.AskToUpdateLinks = false;
            xlWorkbook.UpdateLinks = NetOffice.ExcelApi.Enums.XlUpdateLinks.xlUpdateLinksAlways;

If you're using excel 365 you'll still get privacy levels warnings, and remember that DisplayAlerts = false; doesn't disable security/privacy warnings.

To stop privacy level warnings on 365 you need to disable privacy firewall on power query:

  1. Open your Excel workbook.
  2. From the ribbon, select Data > Get Data > Query Options.
  3. On the left-hand side, under Global, select Privacy
  4. Select Always Ignore Privacy level settings.

Setting the 'AutomationSecurity' to low via code was supposed to do this, but it wouldn't work for me.

For other warnings about external data, something you can try is disable it yourself on your server's excel configurations:

https://support.microsoft.com/en-gb/office/enable-or-disable-security-alerts-about-links-and-files-from-suspicious-websites-a1ac6ae9-5c4a-4eb3-b3f8-143336039bbe

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