22

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

ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
Bruce Dou
  • 4,673
  • 10
  • 37
  • 56

5 Answers5

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.

Community
  • 1
  • 1
dotNetkow
  • 5,053
  • 4
  • 35
  • 50
  • 3
    I have set this, but there also a popups for read-only confirm box etc. – Bruce Dou Apr 08 '11 at 04:29
  • 1
    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.Open](http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.open(v=office.11).aspx) – dotNetkow Apr 08 '11 at 14:58
  • 9
    As of Interop 2013 You can not use true or false, instead use Word.WdAlertLevel.wdAlersNone / ... – CantGetANick Sep 09 '13 at 08:28
  • @dotNetkow i have set the readonly proerty to true after that the code stop working :( – user3217843 Nov 24 '14 at 12:52
  • @user3217843 are you using 2013? see CantGetANick's response above. Different value to use. – dotNetkow Nov 24 '14 at 16:47
  • @dotNetkow i am using word 2010. – user3217843 Nov 25 '14 at 03:48
  • 1
    You're great! I read dozens of non helpful answers, and had to waste a lot of precious time, before eventually finding your answer... – Ofer Sep 25 '16 at 14:38
  • @CantGetANick I used your solution and used Document.OpenNoRepairDialog too, to open the document, but still prompt appears. – Aniket Bhansali Dec 12 '17 at 07:35
  • @AniketBhansali I have no other solution :) Good luck – CantGetANick Feb 09 '18 at 12:31
  • 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. – Hemanth Bidare Aug 23 '19 at 12:02
  • @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. – nam Nov 24 '19 at 00:03
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.

Daniel Bickler
  • 1,119
  • 13
  • 29
Uriel Fernandez
  • 101
  • 1
  • 5
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.

ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
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);
KUL
  • 391
  • 2
  • 15
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.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
Azhar
  • 13
  • 3