1

I have a windows form that works fine on my machine, I have a reference to the Microsoft.Interop Assembly for Excel 2007. Certain machines will have both Excel 2000 and Excel 2007 installed, and if Excel 2007 is not set as the "Default" version of Excel the windows form will throw an error and not work. Is their a way I can "package" my windows form to allow it to run error free on a machine with both 2000 and 2007 installed?

Bob Goblin
  • 1,251
  • 3
  • 16
  • 33
  • I think this is going to be hard because the interop DLL is loaded from the GAC so you can't "deploy" the DLL - it has to already be there. If you don't need to "interact in real time" with Excel there are libraries that let you read/write Excel files without invoking Excel see http://stackoverflow.com/questions/9155642/how-to-read-an-excel-file-in-c-sharp-without-using-microsoft-office-interop-exce – Charlie Jul 09 '15 at 13:03
  • Ugh, a 15 year old version of Excel, what could possibly go wrong? Sure, don't tell us, it doesn't matter. – Hans Passant Jul 09 '15 at 13:16
  • @HansPassant - what information do you need to clarify? – Bob Goblin Jul 09 '15 at 13:29
  • @Charlie - unfortunately I do need to interact in real time so I am stuck using the Interop dll – Bob Goblin Jul 09 '15 at 13:29

1 Answers1

2

You're currently using Early Binding to access Excel. This is where you embed your reference to the interop in the project before compiling.

Advantages

  • Version of Excel is known to the compiler
  • Faster execution as we know that the methods exist and where they are
  • Allows us to use intellisense to check arguments are correct and that return types are correct

Disadvantages

  • Supports only the embedded versions of Excel

You need to take a look at Late Binding. This is where the interop is not included in the project at all and instead is bound later through reflection. You basically trade off all the advantages above for being able to support multiple versions of Excel.

There's lots of guides online that talk about how to do it.

How to use use late binding to get excel instance?
C# : Late Binding Excel Interop Tutorial
Word Automation using Late binding - Usage of dynamic Keyword

My two cents

I must echo Hans comment, this is a bad idea. Trying to support a 15 year old version of Excel is a recipe for massive headaches and I think you'll quickly look to avoid doing this.

Unless you're absolutely set on doing it this way, maybe you can look at a third party library like EPPlus. Or if you only need to read the sheet, do it some alternate way like ODBC and avoid this altogether.

Community
  • 1
  • 1
Equalsk
  • 7,954
  • 2
  • 41
  • 67