Introduction
It is common to need to work with various Office applications using the Microsoft.Office.Interop.xxx
namespaces where xxx
is the Office application, e.g. Microsoft.Office.Interop.Word in order to do so, you must ensure that the appropriate application is installed on the client machine.
Background
Having read both of Tadit's excellent tips I decided that I would make a simple utility class to handle the logic for me - allowing me to reuse the code and optomise it over time if needed.
Using the code
The utility class was added to my standard library, so that I can use it in multiple projects.
using System;
namespace Me.Office.Common
{
public static class OfficeUtils
{
public static bool isWordInstalled()
{
return isApplicationInstalled("Word.Application");
}
public static bool isExcelInstalled()
{
return isApplicationInstalled("Excel.Application");
}
private static bool isApplicationInstalled(string applicationName)
{
Type officeType = Type.GetTypeFromProgID(applicationName);
return officeType != null;
}
}
}
This can be used in any other project as long as it is referenced correctly
using Me.Office.Common;
namespace Me.TestHarness
{
class Program
{
static void Main(string[] args)
{
bool isWordInsatlled = OfficeUtils.isWordInstalled();
bool isExcelInsatlled = OfficeUtils.isExcelInstalled();
}
}
}
History
- 23 December 2013 - First version published .
- 24 December 2014 - Fixed namespace reference