Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using Microsoft.Office.Interop.Excel to write data to an excel file in C#. I am using Excel 2010, so I have Microsoft Excel 14.0 Object Library added as reference.. But now I need to make the program work on Excel 2003 and above. So, I removed my reference to Microsoft Excel 14.0 Object Library and now looking for a way to check the installed Excel version on the machine then use it's COM object library.

Currently, I use this code for checking the version, but it returns "2007" instead of "2010". What am I doing wrong?

 namespace OfficeVersionCheck
{
    using System;
    using Microsoft.Win32;

    class Program
    {
        static void Main(string[] args)
        {
            RegistryKey localMachine = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Office\");

            string version = string.Empty;

            foreach (string key in localMachine.GetSubKeyNames())
            {
                if (key == "11.0")
                    version = "2003";
                else if (key == "12.0")
                    version = "2007";
                else if (key == "14.0")
                    version = "2010";

                if (!string.IsNullOrEmpty(version))
                {
                    break;
                }
            }

            Console.Write(version);

            Console.ReadKey();
        }
    }
}

Now after checking the version, I need to load its COM object library to use in my program. Is there a way to do this? Any help would be appreciated. Thanks in advance.

share|improve this question
Why don't you just reference the "old" Excel 2007 PIA/Library? This will work on all subsequent versions of Excel. – Simon Mourier 2 days ago
I only have Excel 2010 installed on my machine but I need to make my program to work on Excel 2003 and 2007. So, I'm thinking of adding the library programatically based on the Excel version installed in a particular machine. – jaqui 2 days ago
I understand but it's over complicated IMHO. Just grab an old Excel PIA (microsoft.com/en-us/download/details.aspx?id=18346), and you don't have to do all this. – Simon Mourier 2 days ago
I guess. But I'm required to do so.. I need help.. – jaqui 2 days ago
I tried to install O2003PIA.MSI, but it says that I have to install MS Excel 2003 first.. How can I reference this older Excel 2003 PIA/Library? – jaqui 2 days ago
show 7 more comments

1 Answer

You have both 12.0 and 14.0 keys in your registry.

For the second part of your question consider using the late binding. For more information about using late binding in C#, see KB 302902: Binding for Office Automation Servers with Visual C# .NET.

Search for Late Binding produced another good article here.

share|improve this answer
Oh thanks! Now, I would like to ask if there is a way to load the COM object library for the particular version of Excel to use on my project, so I can write on an Excel file. How could I do this? – jaqui May 9 at 5:18
@jaqui Updated my answer for late binding suggestion. – Jacob Seleznev 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.