InnoSetup: Checking whether Microsoft Word is running

I am working on an update to the Word uTIlities. As part of the process, I wanted to update the installer to check for whether Word itself is running, as that causes an error in the installation when updating the Word uTIlities (as opposed to a new installation).

I have used Inno Setup right from the start, and have been very happy with its functioning and flexibility. So how to get it to check for Word?

The first tack I chose was based on this Stack Overflow thread—trying to get AppMutex to work. That, however, failed, because even though I tried loading my modification to the recommended code into the Word uTIlities template file’s ThisDocument object, the installer was not detecting it:

Option Explicit
'This is from the InnoSetup Help files:
'[Setup]: AppMutex

'Place in Declarations section:
Private Declare Function CreateMutex Lib "kernel32" _
        Alias "CreateMutexA" _
       (ByVal lpMutexAttributes As Long, _
        ByVal bInitialOwner As Long, _
        ByVal lpName As String) As Long

Private Sub Document_Open()
    'Place in startup code (Form_Load or Sub Main):
    CreateMutex 0&, 0&, "Word_uTIlities"

End Sub

Next I found this page, but when the link to the PSVince DLL failed, I stopped right there. It would still be nice to get the installer to offer to close down Word on my behalf, but my experience with other installers not always doing this in the way I would have liked convinced me that asking users to do it manually was not all bad.

So then, after a little more digging, I came across an example in the sample code file that actually does test for the presence of Microsoft Word. Starting with that, it was not a lot of work to modify it to get the Inno Setup script code below, which works quite well, I think. Take note, though, of this when testing while compiling your installer from the Inno Setup IDE—essentially, you have to test it by running the *.exe file.

[Code]
function InitializeSetup(): Boolean;
var
  Word: Variant;
begin
  try
    Word := GetActiveOleObject('Word.Application');
  except
  end;
  
  if VarIsEmpty(Word) then
    Result := True
     //Do nothing, Word is not found
  else begin
    MsgBox('Microsoft Word is running. Please close it before resuming the installer.', mbInformation, mb_Ok);
    Result := False;
  end;
end;

And that’s it! Again, Inno Setup proved itself up to the task, and I am satisfied with the end result.

Share