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

非 Y 不嫁゛ 提交于 2019-12-24 10:55:53

问题


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.


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


来源:https://stackoverflow.com/questions/55636331/microsoft-office-interop-excel-application-invokeing-different-versions-of-exc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!