I already did some search here on SO, on this, this, this and more, but no clue, so I’m going to have to ask you guys.
I’m trying to open an Excel file via Interop, and I have Office 2007 installed, so I’m using Microsoft.Office.Interop.Excel version 14.0.
When I try to open a valid xls file, everything runs fine.
But if I try to open an invalid file (like a bitmap, or an executable) via Interop, Excel opens it without complain or warning.
How can I detect Excel has an invalid workbook file loaded, without blocking with an alert ?
I’m trying to write a unit test of my Excel object reader. One of the cases is to try to open an invalid file.
Some code to help:
Application Creation:
_app = new Application()
{
Visible = false,
DisplayAlerts = false, // <-- I don't want to remove this.
AskToUpdateLinks = false,
};
Workbook opening:
_workbooks.Open(filename);
_workbook = _workbooks.get_Item(1); // 1-based.
_worksheets = _workbook.Sheets;
EDIT:
Just to add 1 more information: Excel loads the invalid file. If DisplayAlerts is set to true, it complains (open a dialog) informing the workbook seems to be corrupted, but if DisplayAlerts is set to false, it loads the file just as below:

I just ended using another approach: Excel workbooks have a property called
FileFormat. When Excel opens an invalid file, it sets the file format to one of the enumerator values of text:And inside this enumeration Excel has the valid files (valid for my purpose):
So I ended using a simple “
or” to filter the kind of file I’d like to import:And now it works. Thanks for your help guys, you set me in the right direction.