Closing Excel from within Delphi -
would assist i'm sure basic error on part please.
my goal open excel spreadsheet (invisibly), populate it, attach email, send , close everything.
it close complete, except excel remains open - in task manager - after process 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 := 'sage@valid-email.co.uk'; idmessage1.from.domain := 'valid-email.co.uk'; idmessage1.from.text := 'sage <sage@valid-email.co.uk>'; idmessage1.subject := 'sage'; try sqlquery1.close; sqlquery1.sql.clear; sqlquery1.sql.add('select comp,dept,e_addr acc_email dept="' + emailquery.fieldbyname('dept').text + '"'); sqlquery1.open; while not sqlquery1.eof begin idmessage1.recipients.emailaddresses := emailquery.fieldbyname('e_addr').text; sqlquery2.close; sqlquery2.sql.clear; sqlquery2.sql.add('select * invoice global_dept_number="' + emailquery.fieldbyname('dept').text + '" order account_ref, stock_code'); sqlquery2.open; oxl := createoleobject('excel.application'); oxl.displayalerts := false; owb := oxl.workbooks.add; while not sqlquery2.eof 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 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 <> '' begin vattach := tidattachmentfile.create(idmessage1.messageparts, vsave + emailquery.fieldbyname('dept').text + '.xlsx'); idsmtp1.connect; idsmtp1.send(idmessage1); idmessage1.recipients.emailaddresses := ''; idmessage1.body.text := ''; end; if idsmtp1.connected idsmtp1.disconnect; end; vattach.free; osheet := unassigned; owb := unassigned; oxl.quit; oxl := unassigned; end; end;
yet excel remains in task manager , process runs few times on day, build in background.
could direct me either solution or how troubleshoot problem?
it may workbook isn't closed when close excel. if workbooks saved can close them workbook.close; otherwise can mark workbooks saved before close them (workbook.saved := true) or when close them can close them with:
workbook.close(false);
the false argument tells workbook close without asking save. once workbooks closed excel should quit properly.
Comments
Post a Comment