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.