• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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?
0
ljee
Asked:
ljee
  • 3
  • 2
1 Solution
 
ljeeAuthor Commented:
Acceptable solution only through automation, I don't want hacks like: monitoring processes, system-wide application hooks.

thx
0
 
Scay7Commented:
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:
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now