Click to See Complete Forum and Search --> : Problems with closing Excel


Antti
June 3rd, 1999, 04:26 AM
My problem is that I can't shut down Excel97 application object. I have tried Quit()-method, but it leaves Excel hanging in the memory and I can see it from my task manager. It will close when I quit my program, but I need to shut it down immediately when it has done the job. Can anyone help me with this? Below is example code.


void ExcelTest(CWnd * _pParemt)
{
using namespace Excel;

_ApplicationPtr pXL;

try {
pXL.CreateInstance(L"Excel.Application.8");

pXL->Visible = VARIANT_TRUE;

WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);

_WorksheetPtr pSheet = pXL->ActiveSheet;

try {
// This one will fail
pSheet->Name = "Market Share?";
} catch (_com_error &e) {
dump_com_error(e);
}

pSheet->Name = "Market Share!";

pSheet->Range["A2"]->Value = "Company A";
pSheet->Range["B2"]->Value = "Company B";
pSheet->Range["C2"]->Value = "Company C";
pSheet->Range["D2"]->Value = "Company D";

pSheet->Range["A3"]->Value = 75.0;
pSheet->Range["B3"]->Value = 14.0;
pSheet->Range["C3"]->Value = 7.0;
pSheet->Range["D3"]->Value = 4.0;

pSheet->Range["A5:A1005"]->Value = (double)5;
pSheet->Range["B5:B1005"]->Value = (double)6;
pSheet->Range["C5:C1005"]->Value = (double)7;
pSheet->Range["D5:D1005"]->Value = (double)8;
pSheet->Range["E5:E1005"]->Value = (double)9;

Sleep(3000);

pBook->Saved = VARIANT_TRUE;
pXL->Quit();
} catch(_com_error &e) {
dump_com_error(e);
}
}