5

For many reasons, I have installed on my laptop MSExcel 2003 and MSExcel 2007. I need both versions to develop specific projects for each version (Document level projects and Application level projects). Now I need to do a WinForm project that opens an Excel file, read a CustomXMLParts and write a new Excel file. I'm using a reference to Microsoft.Office.Interop.Excel that use ..\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll library (it's for Excel 2007). And for this code:

Microsoft.Office.Interop.Excel.Application excelApplication;    
excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass();
string version = excelApplication.Version;

At this point version is "11.0", but I need to open Excel 2007 and it must be "12.0", and then when the program try to get the CustomXMLParts, throws an Exception because this method doesn't exist in 2003.

If I uninstall Excell 2003 it works fine, but I need to work with both (2003 and 2007). When I reinstall Excell 2003, it fails again. I check the property "Specific Version", for the Interop.Excel reference, to be sure that it's true, and I tried to modify the app.config oldVersion="12.0.0.0" to not make compatible with Excel 2003, but nothing happens:

  <assemblyIdentity name="Microsoft.Office.Interop.Excel"  publicKeyToken="71E9BCE111E9429C" culture="neutral"/>
  <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="12.0.0.0"/>

Any idea?

I'm using Visual Studio 2008 and Visual Studio Tools for Office.Thanks in advance.

Oriol Terradas
  • 1,788
  • 2
  • 19
  • 30

2 Answers2

1

Probably not the answer you are looking for, but have a look at this SO question: Trying to do Office Automation with Excel 2007, but keeps using Excel 2003

Community
  • 1
  • 1
Tim Lentine
  • 7,782
  • 5
  • 35
  • 40
1

You could use ExcelPackage on Codeplex for your xlsx documents. Then there is no need or using interop. They have example code for reading and writing xlsx files.

This way you can continue to use interop for 2003 without them interfering.

If you need to use interop with both version you should load the assemblies using each version in a separate AppDomain in order to get them separated.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
  • Did you understand the question? – jwg Apr 11 '13 at 10:53
  • Yes, and I provided a workaround to avoid using interop for xlsx files, thus avoiding the version conflict. – Mikael Svenson Apr 11 '13 at 15:47
  • This workaround goes so far around the problem as to be of little help to anyone that would look at this page. Also your last sentence is wrong. Using different versions of the assemblies won't affect which version of the program is opened, except that using older assemblies and a newer program will fail. – jwg Apr 11 '13 at 16:47
  • jwg: Per the question description it does not seem he needs to open Excel. He uses it to read/write a file. You should be able to invoke the correct interop assembly by pointing to the correct COM class, but perhaps not using PIA. If you have to read/write xlsx files, it's often better and easier to use the OpenXML API instead of invoking Excel. So, I beg to differ that my answer is way off. Answering a problem is just as often to provide a different means to the problem which the asker might not have considered. – Mikael Svenson Apr 11 '13 at 21:15
  • Based on the fact that the title reads 'How to use an specific version of Excel in a C# program', the questioner says "I need both versions to develop specific projects for each version (Document level projects and Application level projects)." my conclusion was that he wants to do Interop and that CustomXMLParts was just an example of something which is not supported by 2003. I also took your answer as an example of '3rdparty free alternatives are always better than MS tools' fundamentalism, perhaps I was wrong. – jwg Apr 12 '13 at 07:09