How to detect whether a Word COM Add-in is connected and, if not, load it
Some background
Since Word 2000, Word has been able to work with COM Add-ins.
A COM Add-in may be installed for the current user or for all users of the machine.
If the add-in is installed for the current user only, there will be registry entries at HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Addins.
If the add-in is installed for all users of the machine, there will be registry entries at both HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\Addins and HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Addins.
Microsoft has some good information about how the registry entries work to manage loading COM add-ins (the article is allegedly about Visual Studio 2010, but most of what it says applies back to Office 2000, and applies to COM Add-ins regardless of what platform they were built on).
That article also explains that the COM Add-ins dialog in the user interface is sometimes unreliable.
The task at hand
In code, I want to know whether a certain add-in is loaded. If it is not loaded, then I want to load it.
The problem
Word provides an Application.COMAddIns
collection, which consists of Word.COMAddIn
objects.
A Word.COMAddIn
object has a read/write .Connect
property. In theory, this is what I need. In practice, it's a bit trickier.
If, and only if, the COM Add-in is installed for the current user, then
- reading the
.Connect
property will return an accurate value - setting the
.Connect
property will load, or unload, the add-in as required.
However, if the COM Add-in was installed for all users, then
- reading the
.Connect
property will always return False - setting the
.Connect
property will, at best, result in an error message, and at worst crash Word.
The real problem is that you can't tell from the Word object model whether a COM Add-in was installed for the current user or for all users.
The solution
There is no direct solution from within the Word object model.
There are two potential workarounds:
- litter your code with 'On Error Resume Next' or its equivalent and use error values to infer that an add-in is installed for all users
- read the registry entries to determine whether an add-in is installed for all users or just the current user.
Created 21 April 2011