In Excel Automation, how to gracefully handle an invalid file format error when opening a file?

I am trying to open a Microsoft Excel file in a C # program using the excelApp.Workbooks.Open () method. Once this happens, if the file format is invalid, this method causes an error message box to be displayed. I do not, however, want that; I want to handle this error gracefully in my own code.

My question is, how should I do this?

The above method does not raise any exceptions that I can catch. Even if it were, it would still be this annoying message box. Therefore, perhaps the only way would be to check the file format before opening it. Is there any other method in the Excel API for such a check?

0


source to share


1 answer


Sorry, I cannot simulate an example of a corrupted xls file using Excel 2007.

Try Application.DisplayAlerts = False before calling Workbooks.Open ...



If the workbook cannot be opened, the return value will be NULL. (i.e. Workbook wkb = Workbooks.Open (....); wkb will be null if DisplayAlerts = False and the file cannot be opened)

This is purely based on what I understand in excel object model.

+2


source







All Articles