If you are building add-ins for Microsoft Word, at the time of add-in installation you might want to check whether user has installed MS Word on his system or not. Based on that, you might want to show a message to the user or take proper action as per your business need.
In this small blog post, we will learn what needs to be done using C# to find out whether the said Microsoft Office product is installed or not. Continue reading to know about this coding tricks.
It’s easy to check manually whether any Microsoft Office product is installed or not. But this does not work all time. If you are building an add-in, you must ship the DLL as an installer and in that case, it is require to check the availability of the office app just before the installation.
There are various ways to do so and one of the trick is to play with the Windows Registry to find out whether it is installed. We will not go in this way as that is little more lengthy. Why to go in a difficult way, when we already have a simple coding trick. Click to know how to check whether Outlook is installed in user’s system.
Every office app has a Prog ID and you can retrieve it by calling GetTypeFromProgID method of the abstract class Type and passing the parameter of the office application name. GetTypeFromProgID gets the type associated with the specified program identifier. If progID is a valid entry in the registry and a type is associated with it, that means that the said application is present in that system; otherwise not.
Here is the code snippet for your reference:
var officeApplicationType = Type.GetTypeFromProgID("Word.Application");
if (officeApplicationType == null)
{
// MS Word is not installed in user's system
}
else
{
// MS Word is installed in user's system
}
Make sure that, you have referenced the DLL: Microsoft.Office.Interop.Word in your project; otherwise this will not work.
I hope that, though it’s a small post but will help you to do the same in your project and you can do this very easily after reading and understanding the code shared above. Don’t hesitate to ask your question. I will try to help you as soon as possible. Connect with me over social networking sites like Twitter, Facebook, Google+ to receive all the updates that I share there. Happy coding!!!