Determine when an Excel workbook is closed with Delphi

The following code opens the document specified by the app parameter, and then waits until a specific document is closed. This is great for all types of documents, except when an Excel workbook opens and another Excel workbook opens. The code believes that the document is closed when it is actually open. How can i solve this?

procedure RunAppAndWAit( a: TApplication; app, par, verb: string);
var
  seinfo: tshellexecuteinfo;
  exitcode: dword;
begin
  fillchar( seinfo, sizeof( seinfo), 0);
  seinfo.cbsize := sizeof( tshellexecuteinfo);

  with seinfo do
  begin
    fmask := see_mask_nocloseprocess;
    wnd := a.Handle;
    lpfile := pchar( app);
    lpDirectory := pchar( ExtractFileDir( app));
    lpParameters := pchar( par);
    lpVerb := pchar( verb);

    nshow := sw_shownormal;
  end;

  if ShellExecuteEx( @seinfo) then
  begin
    repeat
      a.ProcessMessages;
      GetExitCodeProcess( seinfo.hprocess, exitcode);
    until ( exitcode <> still_active) or a.terminated;
  end
  else
    sshowmessage( 'Unable to open ' + app);
end;
+3
source share
2 answers

Your attempt only works for applications that open the document in the same process that launches the document.

: , / , .

API, ( Excel, , COM API, Excel), , Excel .

API, , , , .

+4

, , ( ?), Windows EnumWindows, , Excel . (, , Excel.)

, , "Excel" , Excel .

, . , , , . , , ...

Google "EnumWindows Delphi" .

... , - . , API Excel. , CreateOLEObject unAssigned . ( ... , Excel ..). Excel , IMO. , (, "", " " Excel?), .

, , , , Excel . ( , ... , , , .)

, :

  uses ComObj;

  function FindWorkbook( Workbookname: String):boolean;
  var
    ExcelOLE: Variant;
    WorkbookNumber: Integer;
  begin
    Result := FALSE;
    ExcelOLE := CreateOLEObject('Excel.Application');
    try
      for WorkbookNumber := 1 to ExcelOLE.Workbooks.Count do
        if UpperCase(WorkbookName) = UpperCase(ExcelOLE.Workbooks[WorkbookNumber].Name) then
          Result := TRUE;
    finally
      ExcelOLE := unAssigned;
    end;
  end; 
+1

Source: https://habr.com/ru/post/1775982/


All Articles