1

I have the following issue:

var oExcelApp = new Microsoft.Office.Interop.Excel.Application();

On this machine this starts Excel 2016, however I have both Excel 2010 and Excel 2016 installed on my machine. I'd like to start 2010 instead, and I'd like to keep both 2010 and 2016 installed on my machine when I do that.

According to this post, it's not possible. However, my understanding is that you can do this programmatically using the following commands:

(To register Excel 2010 as the default application)

"C:\Program Files (x86)\Microsoft Office\Office14\Excel.exe" /regserver

However when I run this command, all it does is open excel, the desired effect is not observed. Is there a way to do this, maybe with some sort of registry change? Or referencing a different version of libraries?

Update 1

I've tried this too:

var oExcelApp = (Microsoft.Office.Interop.Excel.Application)Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application.14"));

Doesn't seems to work either, disregards the excel version and runs 2016 irregardless of the specified version.

David Rogers
  • 2,601
  • 4
  • 39
  • 84
  • You'd need to start a Process, using the path to the required EXE for the version, then pick up the process for interop. See this very useful blog article by Andrew Whitechapel https://blogs.msdn.microsoft.com/andreww/2008/11/30/launching-office-apps-programmatically/ – Cindy Meister Apr 11 '19 at 18:06

1 Answers1

1

This seems to work quite well. Not sure what the difference between "_Application" and "Application" is, but per the comment "Application" is preferred:

string pathToTheVersionOfExcel "...";
int amountOfTimeToWaitForFailure = 5000;

Process process = new Process();
process.StartInfo.FileName = pathToTheVersionOfExcel;
process.Start();

Thread.Sleep(amountOfTimeToWaitForFailure);

oExcelApp = (Application)Marshal.GetActiveObject("Excel.Application");
David Rogers
  • 2,601
  • 4
  • 39
  • 84
  • I think you'd need to cast as `(Excel.Application)` rather than just `Application` as that should/could refer to the C# application in which this code is running. `_Application` vs `Application` see https://stackoverflow.com/a/40766509/3077495 – Cindy Meister Apr 12 '19 at 07:28