Why can't set cast an object from Excel interop?

Posted by AngryHacker on Stack Overflow See other posts from Stack Overflow or by AngryHacker
Published on 2010-04-22T23:30:30Z Indexed on 2010/04/22 23:33 UTC
Read the original article Hit count: 339

Filed under:
|
|
|

Trying to get a reference to the worksheets (using Excel interop):

Excel.Application xl = new Excel.ApplicationClass();
Excel.Workbooks xlWorkBooks = xl.Workbooks;
Excel.Workbook xlWorkBook = xlWorkBooks.Open(fileName, 0, false, 5, "", 
                      "", true, Excel.XlPlatform.xlWindows, "\t",
                      false, false, 0, true, 1, 0);

Excel.Worksheets xlWorkSheets = (Excel.Worksheets) xlWorkBook.Worksheets;  // crashes

The error is that it cannot cast it:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Worksheets'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208B1-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Is my cast incorrect?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .net-2.0