Link to home
Start Free TrialLog in
Avatar of classmate
classmateFlag for Norway

asked on

How do i test wether MS Excel and MS Word are installed on the host machine OS (windows, office 97 / 2000) in D2006/win32 ?

A task given to me specifies that i should make a series of outprints based on a list of files. If the files are of other types than rtf, .doc and .xls, shellexecute is to be used to create outprints. If the file type is either .rtf or .doc, MS Word should be printing it by the use of OLE, if and only if MS Word is installed on the host machine. Else, shellexecute should be used so that Wordpad can start and print the document out instead. The same will probably apply to .doc files. Xls files will also be opened by shellexecute if Excel is not installed.

Do anyone know how to check if MS Word and MS Excel is installed? And can it be done using COM/OLE functions?

regards
classmate
Avatar of 2266180
2266180
Flag of United States of America image

Hi classmate,
you can rely on registry for this:

open the location: HKEY_CURRENT_USER\Software\Microsoft\Office\
if not failed, then iterate through keys 7.0 to 11.0 and check which one has Word\Options and in it the PROGRAMDIR key

using ole, you can use the trial and fail scheme: try to create an instance of the excel/word application. if not fail, it means that it exist and you can continue. if fail, then drop on the shellexecute. something like:

try
  create applications
  optional check if applications created correctly
  ok:=true;
except
  ok:=false;
end;
  if ok then use ole
         else shellexecute

Cheers!
Avatar of classmate

ASKER

hi ciuly!

In the OLE solution, how could i check if the applications were created correctly? Is it not guaranteed that an exception will be raised if word/excel is not installed?
Could the resulting object be set to nil?

In the registry approach, is the same solution applicable to excel?
with the registry, the word solutino will not work for excel. I don't have a machine where excel is not installed in order to see if an "excel" key is created, but you could test and if excel is not installed and in registry no "excel" key appears (at the same level as word) then checking for existance of "excel" key will be sufficient.

regarding the ole, you could check for the object to be nil, any exceptions beeing thrown and if the var is empty. can't think of anything else. never had this issue before :)
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Rllibby,

Thanks!

I'll spend some time digging into it during next week or perhaps tomorrow.



No problem...

One slight correction for the CreateObject implementation:

function TMSOfficeApp.CreateObject(out OleObject: OleVariant): Boolean;
var  pvDisp:        IDispatch;
begin

  // Set out parameter
  OleObject:=Unassigned;

  // Attempt to create instance of object (IDispatch)
  if (CoCreateInstance(ProgIDToClassID(GetProgrammaticID), nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IDispatch, pvDisp) = S_OK) then
  begin
     // Resource protection
     try
        // Set out parameter
        OleObject:=pvDisp;
        // Success
        result:=True;
     finally
        // Release local interface
        pvDisp:=nil;
     end;
  end
  else
     // Failed to instantiate
     result:=False;

end;

I am also providing some sample code to help when you get around to this:
-----
var  msoOffice:     TMSOffice;
     msoaApp:       TMSOfficeApp;
     ovWord:        OleVariant;
begin

  // Create office wrapper
  msoOffice:=TMSOffice.Create;

  // Resource protection
  try
     // Check for valid path
     if not(msoOffice.Valid) then
        // Unable to detect an install
        ShowMessage('Unable to detect any office applications!')
     else
     begin
        // Determine if word can be detected (can also do the same thing for excel, access, etc...
        if not(msoOffice.Installed[oaWord]) then
           // Unable to find an installed version of app
           ShowMessage('Unable to detect MS Word!')
        else
        begin
           // Create a wrapper around the office application (**DO NOT MANUALLY FREE**)
           msoaApp:=msoOffice.OfficeApp[oaWord];
           // Show path to word
           ShowMessage(msoaApp.FileName);
           // Attempt to create an instance of the ole server
           if msoaApp.CreateObject(ovWord) then
           begin
              // Created instance, now show it
              ovWord.Visible:=True;
              // Release COM reference
              ovWord:=Unassigned;
           end
           else
              // Unable to create instance
              ShowMessage('Failed to create instance!');
        end;
     end;
  finally
     msoOffice.Free;
  end;

end;