How to check, programmatically, if MS Excel exists on a pc?
Solution 1
Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType == null)
{
//no Excel installed
}
else
{
//Excel installed
}
Solution 2
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;
Solution 3
As a quick fix you could just catch the exception and implement proper error handling. Then you can inform the user there.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
Christos Karapapas
Updated on July 17, 2022Comments
-
Christos Karapapas almost 2 years
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?
-
Zenwalker over 12 yearsMuch simpler way is to check registry. Read stackoverflow.com/questions/244517/…
-
ckittel over 12 yearsCould 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?
-
Christos Karapapas over 12 yearswell 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
-
Bob Jarvis - Слава Україні over 12 yearsI'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.
-
Luis over 5 years@BobJarvis I guess he want to use excel interop.
-
-
Christos Karapapas over 12 yearsthat 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
-
ckittel over 12 years@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.
-
Larry almost 10 yearsDownvoted 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)
-
PreguntonCojoneroCabrón about 6 years"If you want to use Microsoft.Office.Interop.Outlook, then the component should be installed in the system. Otherwise, it won't work."