简体   繁体   中英

Microsoft.Office.Interop.Excel.Application - Invokeing Different Versions of Excel

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 .

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM