How to disable popups when opening a file using Microsoft.Office.Interop
Asked Answered
H

6

22

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

Heder answered 7/4, 2011 at 2:0 Comment(0)
G
32

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.

Genovevagenre answered 7/4, 2011 at 13:28 Comment(11)
I have set this, but there also a popups for read-only confirm box etc.Heder
Is the read-only confirmation box your main concern? If so, have you tried opening the document with ReadOnly set to false and/or IgnoreReadOnlyRecommended set to true in the open() method? See here for what I'm referring to: Workbooks.OpenGenovevagenre
As of Interop 2013 You can not use true or false, instead use Word.WdAlertLevel.wdAlersNone / ...Cosmetician
@Genovevagenre i have set the readonly proerty to true after that the code stop working :(Phytophagous
@Phytophagous are you using 2013? see CantGetANick's response above. Different value to use.Genovevagenre
@Genovevagenre i am using word 2010.Phytophagous
You're great! I read dozens of non helpful answers, and had to waste a lot of precious time, before eventually finding your answer...Spoor
@Cosmetician I used your solution and used Document.OpenNoRepairDialog too, to open the document, but still prompt appears.Earthshaker
@AniketBhansali I have no other solution :) Good luckCosmetician
Microsoft Word The last time you opened 'Sample.docx', it caused a serious error. Do you still want to open it? I get above message and my program stop working. Have you got anything similar.Miyamoto
@BruceDou Do make sure that you enable the DisplayAlerts back after your code so it's not disabled for other tasks where you may want usera to be warned before taking any actions where warning is necessary.Scurry
C
6

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.

Charlacharlady answered 13/7, 2017 at 15:9 Comment(0)
T
2

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.

Thorlay answered 31/10, 2018 at 0:28 Comment(0)
K
2

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);
Kalakalaazar answered 29/7, 2020 at 1:25 Comment(2)
That's a great addition, thanks for sharing!Charlacharlady
Will not work if machine is running excel 365Causey
G
0

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.

Geosyncline answered 15/4, 2017 at 6:59 Comment(2)
What does this answer add on top of the already posted other answer?Fleabag
this does not work... I get popup error executing a macroCerecloth
C
0

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

Causey answered 25/1 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.