29

I have an application that needs MS Excel to run, otherwise it crashes. So I want to check and warn the user in case Excel is not installed on user's machine.

How do I do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Christos Karapapas
  • 1,018
  • 3
  • 19
  • 40
  • Much simpler way is to check registry. Read http://stackoverflow.com/questions/244517/where-is-a-reliable-registry-key-to-find-install-location-of-excel-2007 – Zenwalker Aug 19 '11 at 14:43
  • Could you tell us a little bit more about "it crashes?" Maybe include some relevant code in the area that crash so we can get a sense of your usage/needs? – ckittel Aug 19 '11 at 15:03
  • well the crash is excpeted, since there is no excel installed, the error is this:"Retrieving the COM factory with CLSID {00024500-0000-C000-0000000000046}failed due to the following error: 80040154 Class not registered(Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."a try catch does great job in this case as you mentioned below [i just tried it], however i just wanted to make the code a bit more "targeted" and search just for the existance of Excel.the need here is to warn the user that in the current fase the project needs ms excel and don't try to open form1 so this msg is avoided – Christos Karapapas Aug 19 '11 at 15:34
  • 1
    I'm curious - is there a reason why you're looking for Excel? Would it be "just as good" if the user has another spreadsheet (e.g. OpenOffice Calc) which could read the spreadsheet you're trying to open? If the latter, try executing the spreadsheet file itself - Windows should take care of figuring out what application is registered to handle the particular file type and launch it for you. – Bob Jarvis - Слава Україні Nov 28 '11 at 18:05
  • @BobJarvis I guess he want to use excel interop. – Luis Dec 13 '18 at 00:16

5 Answers5

47
Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType == null)
{
     //no Excel installed
}
else
{
     //Excel installed
}
Alec Sanger
  • 4,442
  • 1
  • 33
  • 53
3

As a quick fix you could just catch the exception and implement proper error handling. Then you can inform the user there.

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
3
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;  
   }  
} 

this will do the trick, just do it for all versions

and it can be done like this also

RegistryKey key = Registry.ClassesRoot;
RegistryKey excelKey = key.OpenSubKey("Excel.Application");
bool excelInstalled = excelKey == null ? false : true;
Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
  • 2
    Downvoted because loading an assembly just to see if it's installed is grossly inefficient (and you also can't unload it without doing it in its own appdomain) – Larry Jun 06 '14 at 11:12
  • "If you want to use Microsoft.Office.Interop.Outlook, then the component should be installed in the system. Otherwise, it won't work." – PreguntonCojoneroCabrón Mar 31 '18 at 09:24
3

This doesn't answer your specific question, but does tackle it from an alternate direction...

Does it really need MS Excel to be installed, or do you need the computer to simply be able to display Excel files? For example, if the user has LibreOffice or another similar Excel-file compatible application installed, would that be acceptable?

We have an application that opens Excel files and PDF files for the user. We don't really care what software they user has on their computer to view these files. That's not really our concern. We simply Process.Start(...) the file and let the OS take it from there.

We do wrap the call in a Try/Catch block and offer the user suggestions if this call results in an error; suggestions, like that they may not have Office (Excel) installed, or they are missing a PDF viewer. Basically, instead of proactively trying to identify if the user's computer is in a complete enough state to perform the action, we assume it is, but then handle the situation when it's not once we have discovered it.

ckittel
  • 6,478
  • 3
  • 41
  • 71
  • that is a nice way to handle many cases in my application and i use it in half of my methonds, however there are some methods that use interop to write in worksheets and unfortunatelly that needs ms office. i know there must be a way to do this also with libre and open office but this must need a different .dll different file formats and generally totally different aproach. However i have in my future plans to make my app available to those who have only open or libre office – Christos Karapapas Aug 19 '11 at 15:08
  • @chris Indeed, if you are going to be doing any interop with the native libraries/apis, then my suggestion is wide-of-the-mark. As you noted, my suggestion only really works when displaying, not creating these files. – ckittel Aug 19 '11 at 15:39
2

This blog post here describes how to verify if Excel is installed via the registry (VB.NET code but can easily be converted to C#). Basically, you are going to verify via the HKEY_CLASSES_ROOT\Excel.Application\CurVer key.