<ol>
<li>LaunchボタンはExcelプロセスを作成し起動する。</li>
<li>CheckボタンはExcelプロセスの実行を確認する。</li>
<li>TermボタンはExcelプロセスを通常に終了させようと試みる。<br />ただし、この行為が失敗した場合には、強制終了を行う。</li>
</ol>
<pre class="brush: delphi" title="Excel起動と終了のサンプル">unit UXlsOpenTerm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComObj, StdCtrls;

type
  TForm1 = class(TForm)
    btnLaunch: TButton;
    btnCheck: TButton;
    btnKill: TButton;
    procedure btnLaunchClick(Sender: TObject);
    procedure btnCheckClick(Sender: TObject);
    procedure btnTermClick(Sender: TObject);
  private
  public
    FXls: OLEVariant;
    FXlshWnd: THandle;
  end;

var
  Form1: TForm1;

implementation

{&#36;R *.DFM}

uses
  Variants;

procedure TForm1.btnLaunchClick(Sender: TObject);
begin
  FXls := CreateOleObject('Excel.Application');
  FXls.Visible := true;
  FXls.WorkBooks.Add;
  FXls.WorkBooks.Close;
  FXlshWnd := FXls.hwnd;
end;

procedure TForm1.btnCheckClick(Sender: TObject);
var
  nRes: DWORD;
  nRet: longint;
begin
  nRet := SendMessageTimeout(FXlshWnd, WM_NULL, 0, 0,
    SMTO_ABORTIFHUNG OR SMTO_BLOCK, 1000, nRes);
  if (nRet &gt; 0) then
    ShowMessage('正常処理中!!')
  else
    ShowMessage('返事がない、ただの屍のようだ...');
end;

procedure TForm1.btnTermClick(Sender: TObject);
var
  ProcessID: DWORD;
  Process: THandle;
  nRes: DWORD;
  nRet: longint;
begin
  GetWindowThreadProcessId(FXlshWnd, @ProcessID);
  FXls.Quit;
  FXls := unassigned;

  nRet := SendMessageTimeout(FXlshWnd, WM_NULL, 0, 0,
    SMTO_ABORTIFHUNG or SMTO_BLOCK, 1000, nRes);
  if (nRet &gt; 0) then
  begin
    Process := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessID);
    TerminateProcess(Process, 0);
  end;
end;

end.</pre>