Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Excel 2010 Workbook Open error

Tags:

c#

excel-2010

We recently upgraded from Excel 2007 to Excel 2010, and we have found that existing code started failing.

Exception Message:

Office has detected a problem with this file. To help protect your computer this file cannot be opened.

We have traced this to the line where we open the file

excelApp.Workbooks.Open

Even when opening the file manually, the Protected View Messagebox comes up.

How can we work arround this using C#.

like image 392
Conrad Lotz Avatar asked Aug 15 '12 13:08

Conrad Lotz


1 Answers

Have a look at using Application.FileValidation Property (Excel) before your Open statement.

Returns or sets how Excel will validate files before opening them. Read/write

Files that do not pass validation will be opened in a Protected View window. If you set the FileValidation property, that setting will remain in effect for the entire session the application is open.

You can set it to one of the enum values in MsoFileValidationMode Enumeration

msoFileValidationDefault

msoFileValidationSkip

if you set it to msoFileValidationSkip before the Open statement, it should bypass the check.

Something like

excelApp.FileValidation = MsoFileValidationMode.msoFileValidationSkip;

before the open statement.

like image 88
Adriaan Stander Avatar answered Sep 21 '22 21:09

Adriaan Stander