A few months ago, I wrote a post on fixing Word 2007 add-in issues with a registry merge. In this post, I’ll take that idea a little further and explain how to automatically detect and fix add-ins through the use of a macro that runs each time Word is opened and a batch file that runs the registry merge file. All the end-user needs to do to repair the missing functionality is close and reopen Word.
The idea is that, in a corporate environment, there are certain important add-ins that must be running in order for Word to work normally. A good example would be an add-in that integrates Word with a document management system. Should that integration be lost because the add-in failed to load, data may be lost. Because there is no way to force Word to load certain add-ins, and there is no built-in function in Word for warning users when critically important don’t load, I decided to come up with a method for alerting the user to the problem and then fixing it with as little inconvenience as possible.
The example code in this post assumes the workstation is running Office 2007 on Windows XP (32-bit). I would think that the method could be adapted to other environments (Windows 7, Office 2010) without too much trouble. I’ve tried to note where differences come up on Windows 7 and 64-bit operating systems.
The process has four components:
- an autoexec Word 2007 macro that runs each time Word is opened
- a batch file that runs the registry merge file and writes an entry to a log file
- the registry merge file that contains the correct LoadBehavior settings for the add-ins
- a text file that acts as a log
The macro can be added to Normal.dotm or saved to a new .dotm file placed in the startup directory. The .bat batch file, .reg registry file, and .txt log file can be put anywhere, but in this example, they will be saved to a new folder C:\Word Add-ins fix\.
In the code examples below, I’ll be using the Acrobat PDFMaker Office COM Addin as the add-in that must always be loaded. This plugin is installed with Acrobat versions 8.1 and above and adds an Acrobat tab to the ribbon.
The macro
The first thing to do is to collect some information on the COM add-ins that are currently installed. Microsoft Support provides a simple macro for listing all of the COM add-ins at Some COM add-ins are not listed in the COM Add-Ins dialog box in Word. I recommend using this macro to identify the ProgID of your add-ins. The text to the left of the hyphen is the add-in’s Description and the text to the right of the hyphen is the ProgID.
Running the macro from the Microsoft site shows us that the ProgID for the Acrobat PDFMaker Office COM Addin is PDFMaker.OfficeAddin.
![Application.COMAddIns-Description-ProgID](//ardamis.com/wp-content/uploads/2010/06/Application.COMAddIns-Description-ProgID.png)
In Microsoft jargon, an add-in with a LoadBehavior of 3 is ‘Connected’. The COMAddIn object has a property called Connect that will be True if the add-in is Connected and False if it is not. The macro first checks the Connect property of each add-in and writes the ProgID of each Connected add-in to a string. It then checks to see if the string contains a match for each of the required add-ins. If the required add-in does not exist in the string, the macro will display a message to the user and fire the batch file to reset the LoadBehavior. It also passes the names of any not connected add-ins to the batch file as a parameter, so that information can be logged.
I found this article from MSDN on the COMAddIn object very helpful.
Sub AutoExec()
'
' FindMissingAddins Macro
' Display a message box with any critical but not 'Connected' COM add-ins
'
Dim msg As String
Dim MyAddin As COMAddIn
Dim i As Integer, stringOfAddins As String
For Each MyAddin In Application.COMAddIns
If MyAddin.Connect = True Then
stringOfAddins = stringOfAddins & MyAddin.ProgID & " - "
End If
Next
' Update the number of elements in the array
' Example: change to "requiredAddIns(0 To 4)" if you were checking 5 total add-ins)
Dim requiredAddIns(0 To 0) As String
' Add each required AddIn to the array
requiredAddIns(0) = "PDFMaker.OfficeAddin"
' requiredAddIns(1) = ""
' requiredAddIns(2) = ""
' requiredAddIns(3) = ""
' requiredAddIns(4) = ""
For Each requiredAddIn In requiredAddIns
If InStr(stringOfAddins, requiredAddIn) Then
msg = msg
Else
msg = msg & requiredAddIn & vbCrLf
listOfDisconnectedAddins = requiredAddIn & " " & listOfDisconnectedAddins
listOfDisconnectedAddins = Trim(listOfDisconnectedAddins)
End If
Next
If msg = "" Then
Else
MsgBox "The following important add-ins are not running: " & vbCrLf & vbCrLf & msg & vbCrLf & vbCrLf & "Please save your work, then close and reopen Word."
' Run a batch file that corrects the add-in problems in the registry and pass the list of unconnected add-ins as an argument
Shell """C:\Word Add-ins fix\fixWordAddins.bat"" """ & listOfDisconnectedAddins & """"
End If
End Sub
Edit the macro to fit your environment. You will need to specify each required add-in’s ProgID in the requiredAddIns array and update the number of add-ins in the array’s definition. The macro needs to be named AutoExec in order to run when Word starts.
This is the message that the user will receive if the macro finds that a required add-in is not Connected.
![alert-on-MyAddin.Connect-is-not-True](//ardamis.com/wp-content/uploads/2010/06/alert-on-MyAddin.Connect-is-not-True.png)
Clicking OK runs the batch file and closes the window.
The batch file
The batch file is called by the macro when any of the add-ins are not registered and currently connected. The batch file references the .reg file that contains the correct LoadBehavior settings and writes an event to the log with information on the username, the machine name, the datetime that the problem was discovered and which add-in(s) were not connected.
Copy the code and save it as fixWordAddins.bat to the C:\Word Add-ins fix\ directory or wherever you want.
:: A batch file for running a registry merge to set the LoadBehavior for Word add-ins
::
@echo off
REGEDIT /S "C:\Word Add-ins fix\fixWordAddins.reg"
:: Let's create a little log file and output which user ran the script and at what date and time
:: Set some variables for the date. US format - not localized!
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)
:: Set some variables for the time.
@For /F "tokens=1,2,3 delims=/: " %%A in ('Time /t') do @(
Set Hours=%%A
Set Minutes=%%B
Set Meridiem=%%C
)
:: Output to a log file
@echo %username% at %computername% on %Month%/%Day%/%Year% at %Hours%:%Minutes% %Meridiem% (%1) >> "C:\Word Add-ins fix\log.txt"
The registry merge
Add-ins can be ‘hard-disabled’ or ‘soft-disabled’ by Word 2007. Please see my post at fixing Word 2007 add-in issues with a registry merge for more information about what this means. The following registry merge will address both issues.
The registry merge will have to be edited for your environment, too. First, find the LoadBehavior value in the the registry for each of your add-ins in either of two locations:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\Addins\
HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Addins\
If you don’t find your add-in in either of those locations, search the registry for the ProgID.
A LoadBehavior of 3 = loaded (this corresponds to a checked box in Word Options/Add-Ins)
A LoadBehavior of 2 = not loaded (this corresponds to an unchecked box in Word Options/Add-Ins)
The only add-ins that you need to worry about here are those that you want to always run (those that normally have a LoadBehavior of 3). Export those keys and add them to the .reg file.
Copy the code and save it as fixWordAddins.reg to the C:\Word Add-ins fix\ directory or wherever you want. Edit it for the add-ins you wish to load.
Windows Registry Editor Version 5.00
[-HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Resiliency]
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Word\Resiliency]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\Addins\PDFMaker.OfficeAddin]
"LoadBehavior"=dword:00000003
On 64-bit versions of Windows, the add-ins can be found in:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\Word\Addins\
The log file
If the log file doesn’t already exist, the batch file will create it. Each time the batch file runs, it adds a line to the end of the log file.
An entry will look something like this:
USERNAME at MACHINENAME on 06/17/2010 at 01:02 PM ("PDFMaker.OfficeAddin")
Caveats
The macros in a *.dotm can’t be edited if that template was opened from the startup location. Open a copy in another location, make your changes and save, and then overwrite the version in the startup location.
Windows 7
Note that in Windows 7, the UAC needs your permission to run a .bat file.
The Word startup location in Windows 7 (put any custom *.dotm files here):
\AppData\Roaming\Microsoft\Word\STARTUP
The default location of Normal.dotm in Windows 7:
\AppData\Roaming\Microsoft\Templates