Such as read-only confirm, other alerts. What to do with these popups? Or ignore them?
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.
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 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.
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.
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);
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.
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:
- Open your Excel workbook.
- From the ribbon, select Data > Get Data > Query Options.
- On the left-hand side, under Global, select Privacy
- 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:
© 2022 - 2024 — McMap. All rights reserved.