unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls;
type
TForm1 = class(TForm)
btnCreate: TButton;
btnQuit: TButton;
procedure btnCreateClick(Sender: TObject);
procedure btnQuitClick(Sender: TObject);
private
{ Private-Deklarationen }
ExcelObj: OleVariant;
procedure close_excel;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnCreateClick(Sender: TObject);
begin
try
try
ExcelObj:=CreateOleObject('Excel.Application');
ExcelObj.Visible:= false;
ExcelObj.DisplayAlerts:=false;
except
on e:
exception do
begin
ShowMessage(e.Message+#13#10+
'Kein Excel installiert?');
exit;
end;
end;
finally
//close_excel; //geht hier nicht, daher raus
end;
//close_excel; //hier auch nicht, daher raus
ShowMessage('pause');
end;
procedure TForm1.btnQuitClick(Sender: TObject);
begin
close_excel; //hier funktioniert es, direkt und ohne die das ShowMessage bestätigen zu müssen
ShowMessage('pause');
end;
procedure TForm1.close_excel;
begin
if (not VarIsEmpty(ExcelObj)) then
begin
ExcelObj.quit;
ExcelObj := unassigned;
end;
end;
end.