'Closing Excel from within Delphi
Would someone assist with what I'm sure is a basic error on my part please.
My goal is to open an Excel spreadsheet (invisibly), populate it, attach it to an email, send and close everything.
It is close to complete, except that Excel remains open - in the Task Manager - after the process is complete.
The block of code is:
procedure TFMain.SendEmail;
var
i, j, r: Integer;
vBody, vSaveVer: string;
vAttach: TIdAttachment;
vMftQty: array [1 .. 2] of Integer;
vQtyTot: array [1 .. 12] of Integer;
vNetTot: array [1 .. 12] of Real;
oXL, oWB, oSheet: Variant;
begin
IdMessage1.From.Address := '[email protected]';
IdMessage1.From.Domain := 'valid-email.co.uk';
IdMessage1.From.Text := 'Sage <[email protected]>';
IdMessage1.Subject := 'Sage';
try
SQLQuery1.Close;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add('SELECT comp,dept,e_addr FROM acc_email WHERE dept="' +
EMailQuery.FieldByName('Dept').Text + '"');
SQLQuery1.Open;
while not SQLQuery1.Eof do
begin
IdMessage1.Recipients.EMailAddresses :=
EMailQuery.FieldByName('E_Addr').Text;
SQLQuery2.Close;
SQLQuery2.SQL.Clear;
SQLQuery2.SQL.Add('SELECT * FROM invoice WHERE global_dept_number="' +
EMailQuery.FieldByName('Dept').Text +
'" ORDER BY account_ref, stock_code');
SQLQuery2.Open;
oXL := CreateOleObject('Excel.Application');
oXL.DisplayAlerts := False;
oWB := oXL.Workbooks.Add;
while not SQLQuery2.Eof do
begin
oXL.Cells[r, 1].Value := 'Code';
oXL.Cells[r, 2].Value := 'Description';
oXL.Cells[r, j * 2 + 1].Value := 'Qty';
oXL.Cells[r, j * 2 + 2].Value := 'Value';
Inc(r);
oWB.Worksheets.Add(After := oWB.Worksheets[oWB.Worksheets.Count]);
oSheet := oWB.ActiveSheet;
oSheet.Name := SQLQuery2.FieldByName('account_ref').Text;
oXL.Cells[1, j * 2 + 1].Value := 'Month';
r := 2;
SQLQuery2.Next;
end;
if oWB.Worksheets.Count > 1 then
oWB.Worksheets.Item['Sheet1'].Delete;
oWB.Worksheets[1].Select;
oWB.SaveAs(vSave + EMailQuery.FieldByName('Dept').Text + '.xlsx');
SQLQuery1.Next;
end;
try
if IdMessage1.Recipients.EMailAddresses <> '' then
begin
vAttach := TIdAttachmentFile.Create(IdMessage1.MessageParts,
vSave + EMailQuery.FieldByName('Dept').Text + '.xlsx');
IdSMTP1.Connect;
IdSMTP1.Send(IdMessage1);
IdMessage1.Recipients.EMailAddresses := '';
IdMessage1.Body.Text := '';
end;
finally
if IdSMTP1.Connected then
IdSMTP1.Disconnect;
end;
finally
vAttach.Free;
oSheet := Unassigned;
oWB := Unassigned;
oXL.Quit;
oXL := Unassigned;
end;
end;
Yet Excel remains in the Task Manager and as the process runs a few times over the day, they build up in the background.
Could someone direct me to either the solution or how to troubleshoot the problem?
Solution 1:[1]
It may be that your workbook isn't closed when you close Excel. If your Workbooks are saved already then you can close them with Workbook.Close; Otherwise you can mark workbooks as saved before you close them (Workbook.Saved := True) or when you close them you can close them with:
Workbook.Close(False);
The false argument tells the Workbook to close without asking to save. Once all Workbooks are closed then Excel should quit properly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MarkF |
