Automation, Word - Assigning OnQuit programatically?

Hi,

The code below fails:

procedure TSomeObject.Quitting(Sender: TObject);
begin
  messagebeep(0);
end;

procedure TSomeObject.SomeProcedure;
var
  FApplication: Variant;
begin
  try
    FApplication := GetActiveOleObject( 'Word.Application );
  except
    FApplication := CreateOleObject( 'Word.Application' );
  end;

  FApplication.OnQuit := Quitting;
  // the above line fails with compiler error 'Not enough parameters'.


  ..........
end;

Actually my goal is to have my application receive a notification when Word is closed. Is that possible? How?
ljeeAsked:
Who is Participating?
 
Scay7Connect With a Mentor Commented:
are you running the word.application from your program, if so...

var
  SEInfo: TShellExecuteInfo;
  ExitCode: DWORD;
  ExecuteFile, StartInString  : string;
begin

  ExecuteFile:= ''; //full path of file to be run
  startinstring:= ''; //full directory path of file to be run

  FillChar(SEInfo, SizeOf(SEInfo), 0);
  SEInfo.cbSize := SizeOf(TShellExecuteInfo);
  with SEInfo do begin
    fMask := SEE_MASK_NOCLOSEPROCESS;
    Wnd := Application.Handle;
    lpFile := PChar(ExecuteFile);
    lpDirectory := PChar(StartInString);
    nShow := SW_SHOWNORMAL;
  end;
  if ShellExecuteEx(@SEInfo) then begin
    repeat
    Application.ProcessMessages;
    GetExitCodeProcess(SEInfo.hProcess, ExitCode);
    until (ExitCode <> STILL_ACTIVE) or Application.Terminated;

    //here is where word would exit...

    end else
          begin
          showmessage('Unable to launch');
          end;
end;



or you can...

uses
  ComObj, ActiveX;

function IsObjectActive(ClassName: string): Boolean;
var
  ClassID: TCLSID;
  Unknown: IUnknown;
begin
  try
    ClassID := ProgIDToClassID(ClassName);
    Result  := GetActiveObject(ClassID, nil, Unknown) = S_OK;
  except
    Result := False;
  end;
end;

someprocedure
begin
  if IsObjectActive('Word.Application') then ShowMessage('Word is running !') else showmessage('ITS GONE!');
  if IsObjectActive('Excel.Application') then ShowMessage('Excel is running !');
  if IsObjectActive('Outlook.Application') then ShowMessage('Outlook is running !');
  if IsObjectActive('Access.Application') then ShowMessage('Access is running !');
  if IsObjectActive('Powerpoint.Application') then ShowMessage('Powerpoint is running !');
end;

Peace Scay7
0
 
ljeeAuthor Commented:
Acceptable solution only through automation, I don't want hacks like: monitoring processes, system-wide application hooks.

thx
0
 
ljeeAuthor Commented:
Thanks Scay7. You got the points, your second alternative with IsObjectActive works fine for me.

This question can be closed (frankly it's a long time since I did not visited EE, I don't rememeber what am I supposed to do now??).

regards,

ljee
0
 
Scay7Commented:
here we go a nice picture of how to accept the answer :P

http://www.experts-exchange.com/help.jsp#hi68

Peace Scay7
0
 
ljeeAuthor Commented:
geee, thanks :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.