开发者

Check installed Excel version and launch it

开发者 https://www.devze.com 2023-04-08 09:25 出处:网络
I am trying in my application to launch Microsoft Excel with specific arguments (ie. additional xla & xll).

I am trying in my application to launch Microsoft Excel with specific arguments (ie. additional xla & xll).

For now it works fine because all of my users only have Office11 (=2003) installed.

My company is going to switch to Windows 7 & Office 2010 and I logically can't launch any excel since the .exe is not located in C:\Program Files:\Microsoft Office\Office11\EXCEL.EXE

I ran a quick check in the registry to see that I can definitely check what version is currently installed. There are also plenty of articles explaining how to get the currently installed Office version.

However, I'd like to know if it is possible to find anything (such as a good registry key) directly giving me the .exe path so as to launch Excel.

Using my current machine (Win XP x86, Office11), I can find it in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Excel\InstallRoot

Using this key I can, basically, find a workaround to get the actual path. Problem: there is no such key in Windows 7's registry with Office 2010 (= Office 14) installed.

Do you guys know any way to launch the currently installed excel from C#?

FYI, here is the current code section, launching Office11 from a x64 / x86 machine:

    private void LaunchExcel(string arguments)
    {
        if (!Is64BitsOS())
        {
            Process process = new Process();
            process.StartInfo.FileName = "excel";
            p开发者_如何学运维rocess.StartInfo.Arguments = arguments;
            process.Start();
        }
        else
        {
            Process process = new Process();
            process.StartInfo.FileName = "c:/Program Files (x86)/Microsoft Office/Office11/excel.exe";
            process.StartInfo.UseShellExecute = false;

            process.StartInfo.Arguments = arguments;
            process.Start();
        }
    }

Any ideas to make this code more generic?


If you start Excel to open an Excel file, you can start a Process with the Excel file as FileName and let the Windows shell do all the work to find the associated application. You'd need an exception handler, obviously.

This would make you independent of Office and Windows versions and registry keys.

Otherwise you could take a different approach and find the associated application, like here.

The point of these suggestions is: presently, you have to change your code as soon as a new Office version is installed or a different Windows version is used, while there is a way to avoid these dependencies.


The 32 bit version of Excel 2010 running on a 64 bit version of Windows (XPx64,Vistax64,Win7x64) will have the following key. I think this is the key you are looking for HKLM\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Excel\InstallRoot

The 64 bit version of Excel 2010 running on a 64 bit version of Windows (XPx64,Vistax64,Win7x64) will have the following key

HKLM\SOFTWARE\Microsoft\Office\14.0\Excel\InstallRoot

copied from here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号