1

I'm trying to open a WorkBook in a C# application. When I call the Open method it returns a System__COMObject instead of a Workbook. Here's my code:

  Microsoft.Office.Interop.Excel._Application excelApp = new Application();
  Microsoft.Office.Interop.Excel._Workbook wb = new WorkBook();      

  wb  = excelApp.Workbooks.Open(filepath, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

The error is an System.InvalidCastException. Any ideas what I'm doing wrong?

EDIT: I don't now install any COM packages or adjust any settings. Do I need any COM components to work with Interop?

ExceptionLimeCat
  • 6,191
  • 6
  • 44
  • 77

3 Answers3

0

Not sure where you are getting System.InvalidCastException but I don't think you can construct a Workbook instance that way. Just simplify to...

Microsoft.Office.Interop.Excel._Workbook wb = excelApp.Workbooks.Open(...);
blins
  • 2,515
  • 21
  • 32
  • I still get the invalid cast with this declaration. – ExceptionLimeCat Sep 27 '12 at 02:51
  • Have you tried explicitly casting to Microsoft.Office.Interop.Excel.WorkbookClass instead? May be related to the version of Excel or the interop assembly you are referencing. – blins Sep 27 '12 at 03:56
  • Yes I've tried casting. I've tried storing the COM_Object in an `object` then casting that object into a WorkBook obj – ExceptionLimeCat Sep 28 '12 at 00:45
  • Not sure... However can you use C# 4 and dynamic? Then you can shed the interop dependencies all together (at the expense of design time type checking but that may be minor depending on how much you are doing) i.e. something like: dynamic excelApp = Activator.CreateInstance(Type.GetTypeFromCLSID("Excel.Application")); – blins Oct 19 '12 at 22:09
0

Ok, you have something odd going on because "WorkBook" in Excel interop is an interface, not a class. "= new WorkBook()" simply should not work.

var excelApp = new Application();
var wb = excelApp.Workbooks.Open( filepath, Type.Missing, Type.Missing, 
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing ) as Workbook;

Should net you what you're after. The types in your declarations (using the _Application) also look a bit suss.

This example uses the COM reference for Excel, also be sure that the option for "Embed Interop Types" is set to "True" in the reference properties.

Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • I changed the declarations to `var` but I still got an invalidcast. – ExceptionLimeCat Sep 27 '12 at 02:50
  • com interop has some magic that lets you call `new` on interface types; there's an attribute that associates the interface with a class, and that class is instantiated by the `new` operator. – phoog Sep 27 '12 at 06:42
0

I after researching intensely on this issue. I discovered there are some known issues with Microsoft.Interop with Excel. I ultimately decided to go with another solution Flexcel.

ExceptionLimeCat
  • 6,191
  • 6
  • 44
  • 77