How can I determine the version of Microsoft.Office.Interop.Excel on a client computer?

10,093

Solution 1

If the user has, for example, Interop.Excel 14.0 installed, does that mean they definitely have Excel installed too?

No, interops are used for marshalling calls from managed programming languages to the unmanaged applications.

How should I check for which version of Excel is installed?

Typically the windows registry is used to detect the installed Office versions. Note, there can be multiple Office versions installed on the PC. Also you may try to run the Office application programmatically and then check out the Version property at runtime. See How to automate Microsoft Excel from Microsoft Visual C#.NET for more information.

We plan to support Excel 2007, 2010, and 2013. In short -- How do I cover all of my bases here?

Use the oldest interop whcih corresponds to the lowest Office version you need to support. Thus, you will be sure that you use methods and properties that are available in all Office versions. Or my embedd interop assemblies (types) into your application. This feature is available when you target .net 4.0 and later.

Solution 2

There is much more simple solution here. In my case I need to seperate my code flows due to Excel Interop versions.

Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
if(oXL.Version.Equals("15.0"))
{
   //bla bla..
}
else if(oXL.Version.Equals("14.0")||oXL.Version.Equals("12.0") || oXL.Version.Equals("11.0"))
{
   //bla bla..
}
Share:
10,093
Mike T
Author by

Mike T

Updated on June 27, 2022

Comments

  • Mike T
    Mike T almost 2 years

    I have an application that needs to use the Microsoft.Office.Interop.Excel assembly (nothing crazy, just simple writing to worksheets).
    At compile time, I am using Microsoft.Office.Interop.Excel 15.0. I have Office 2013 installed and everything works as expected.

    But that's all irrelevant in terms of if the client can use it.

    How can I programatically determine the version of Interop.Excel the user has installed? Once I know what version they have, how can I then load those assemblies at runtime to gain access to the Interop objects? If the user has, for example, Interop.Excel 14.0 installed, does that mean they definitely have Excel installed too? How should I check for which version of Excel is installed?

    I know the user needs Office installed in order to use Interop.Excel objects, so does that mean I need to be checking both the Office version as well as the Interop assembly version?

    We plan to support Excel 2007, 2010, and 2013.

    In short -- How do I cover all of my bases here?

    There are plenty of questions out there about Interop.Excel but nothing that has made it clear to me:

    a) What I need to check for on the client computer

    b) How I do those checks through reflection(avoid registry lookup)

    Thanks.