tags:

views:

645

answers:

2

I need to know whether Microsoft Word, Excel, Outlook, Project, etc are installed in a Windows Forms .net 2.0 C# application.
The first attempt was by simply trying to create the application objects and catching any exception, but this is too time consuming.
Is there any faster way to detect this? Like checking registry values, or another technique with the COM wrappers?

+2  A: 

This should work, as described here. It's not a very elegant solution, though, as it is version specific and will break with the next office version. This example is forOffice 2003, so it won't work with Office 2007 without updating.

const string ASSEMBLY2003 = "Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";  

static bool IsAssemblyInstalled(string assembly)  
{  
   try 
   {  
       s_assemblyExcel = Assembly.Load(assembly);  
       return true;  
   }   
   catch 
   {  
       return false;  
   }  
}
netterdotter
This method actually checks only whether the OFfice PIAs are installed on the system. There can be PIAs without Office and Office (2003, XP) without PIAs.
0xA3
+2  A: 

You can use the MSI (Windows Installer) APIs to find out if the relevant product/package/component codes are present on the machine. These are fairly simple to use via P/Invoke.

Alternatively, you can look in the registry. Word 2007, for example, puts its install location at HKLM\SOFTWARE\Microsoft\Office\12.0\Word\InstallRoot.

This doesn't help you if you're planning on using the interop components, but it does tell you, with reasonable certainty, whether the various things are installed.

Roger Lipscombe