Releasing Excel From Memory


2003-11-28 11:24:23 AM
cppbuilder64
Original Message Subject: Reading the value of a cell in an Excel
Worksheet
Quote
I try to read and write cell's in Excel but Excel hang around in
memory until I close my program. What's wrong?

Until here it is ok:

ExcelApplication1 = new TExcelApplication (NULL);
ExcelApplication1->Connect ();

ExcelApplication1->Workbooks->Open(TVariant(File));

ExcelApplication1->Workbooks->Close ();

ExcelApplication1->Quit ();
ExcelApplication1->Disconnect ();

delete ExcelApplication1;

Excel doesn't hang around in memory


when I add the following line

ExcelApplication1->Workbooks->get_Item(TVariant(1))->Sheets->get_Item(TVariant(1));

Excel hang around in memory.

I have found that calls to get_Item() or get__Default() will require a
call to Release() later on.
I also have the habit of not using more than one pointer (->) in one
statement when dealing with
Excel objects.
Break the line up, something like:
WorkbooksPtr workbooks;
ExcelWorkbookPtr workbook;
SheetsPtr sheets;
ExcelWorksheetPtr sheet;
workbooks = ExcelApplication1->Workbooks;
workbook = workbooks->get_Item(Variant(1));
sheets = workbook->Sheets;
sheet = sheets->get_Item(Variant(1));
...
sheet->Release();
workbook->Release();