I work with Excel in Delphi 7 via OLE. How to create a file, change something in it and close it, there are no problems. But I also need to open the Excel file saved in the database (MSSQL) for viewing. I work with Excel like this:

ExlApp := CreateOleObject('Excel.Application'); try ExlApp.Workbooks.Open(XLSFile); Sheet := ExlApp.Workbooks[ExtractFileName(XLSFile)].WorkSheets[1]; ... finally ExlApp.DisplayAlerts := false; ExlApp.ActiveWorkbook.Close(SaveChanges:=False); ExlApp.Application.Quit; Sheet := Unassigned; ExlApp := Unassigned; end; 

If we only open the Excel file, that is, a piece of code:

  ExlApp.DisplayAlerts := false; ExlApp.ActiveWorkbook.Close(SaveChanges:=False); ExlApp.Application.Quit; Sheet := Unassigned; ExlApp := Unassigned; 

will be absent, is it necessary to clear the memory later (when the user closes the Excel document) allocated for ExlApp and if so, in what way?

  • one
    Be sure to close otherwise Excel will hang in the processes, i.e. how many times create ExlApp, so much will hang. Add more ExlApp.Application.Quit; behind the try-finaly block - Praddos
  • By the way, just for the OLE Compound Document format (that is, for example, XLS, but not XML and not XLSX), you could open a document directly from memory without saving it to disk in between. - Arioch

1 answer 1

In fact, it was necessary to write this:

 try ... finally ExlApp.Visible:=True; Sheet := Unassigned; ExlApp := Unassigned; end; 

We release the memory and the program continues. And the user will close the Excel document himself when he sees fit.