Wednesday, August 25, 2004

Small tip to detect if MS Word is installed on the PC

This piece of code was written for an Italian newsgroup, but I feel like sharing it here.
The basic working is simple:

- It detects if a .doc extension is registered
- If there's any, it goes checking which program is associated with it
- If the program is different from word.exe then it returns false
- Otherwise returns true

Here is the code:

procedure TForm1.Button1Click(Sender: TObject);

var Reg : TRegistry;
KeyToOpen : String;
Value : String;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CLASSES_ROOT;
if Reg.OpenKeyReadOnly( '.doc' ) then
begin
KeyToOpen := Reg.ReadString( '' );
Reg.CloseKey;
if Reg.OpenKeyReadOnly( KeyToOpen+'\shell\open\command' ) then
begin
Value := Reg.ReadString( '' );
if Pos( 'word.exe',Value ) > 0 then
ShowMessage( 'Word is installed' )
else
ShowMessage( 'Word isn't installed' );
end;
end
else begin
ShowMessage( 'Word isn't installed' );
end;
end;
Obviously, you'll have to include the Registry unit in one of your uses clauses,
preferably implementation one( to avoid unnecessary noise in the interface part )

Cheers

3 comments:

Ondrej Kelle said...

Very unreliable, IMHO. You could have another program (e.g. OpenOffice) installed *after* MS Word so it has overtaken the .doc extension registration. Thus, you would have both MS Word and OpenOffice installed on your computer, but your routine would return False.
BTW, I'm not saying that OpenOffice does this; I don't use MS Word anymore ;-)
But, of course, in some cases your solution may be sufficient; I don't have a better one at hand. If it was really important, I would rather try to detect registry, HKLM\Software\Microsoft\Office\... or something like that. Perhaps Word Automation GUIDs under HKCR.

Andrea Raimondi said...

Hi, TOndrej.

I can definitely see your point, but I think that it's anyway very much unlikely you'll be installing them one
after another, and in that order moreover.

Thus, yes, it can surely fail under certain conditions, but they're admittedly quite "exotic".

The option of looking for Word automation GUIDs might be good as well, but do you know of any place listing them?

I think you can see that if I have to copy them at hand, this task is extremely error prone.

Cheers

Ondrej Kelle said...

You could look up the GUIDs in your registry if you have Word installed... Something like ProgID registry entry for "Word.Application" should be quite version-agnostic. Still, not 100% reliable. I'm afraid there's no 100% solution unless it's clear what "MS Word is installed" exactly means. Sorry...