OLE automation crashing Excel


2003-10-08 08:14:30 PM
cppbuilder96
I use OLE automation to create and manipulate an excel file. The creation
and the data manipulation works fine. But when closing the application,
Excel crashes with an error (access violation). I found out that
"ExcelApp->Quit()" does not realy close Excel, it is still listed in
Task-Manager.
Here is the code:
=== ExApp.h ===
...
TExcelApplication *ExcelApp;
TExcelWorkbook *ExcelWbDi;
TExcelWorksheet *ExcelWsDi;
...
=== End of ExApp.h ===
=== ExApp.cpp ===
...
try
{
try
{
ExcelApp->Connect();
}
catch(Exception *e)
{
MessageDlg("Excel may not be installed", mtError,
TMsgDlgButtons() << mbYes, 0);
Abort;
}
if (FileExists(FFileName))
{
DeleteFile(FFileName);
}
ExcelApp->Workbooks->Add(OleVariant(Unassigned), 0);
ExcelApp->get_ActiveWorkbook()->SaveAs(OleVariant(FFileName),
OleVariant(xlWorkbookNormal),
OleVariant(AnsiString("")),
OleVariant(AnsiString("")),
OleVariant(false),
OleVariant(false),
xlNoChange,
OleVariant(xlUserResolution),
OleVariant(true),
OleVariant(Unassigned),
OleVariant(Unassigned),
0);
ExcelWbDi->ConnectTo(ExcelApp->get_ActiveWorkbook());
ExcelWsDi->ConnectTo(ExcelApp->get_ActiveSheet());
/// Writing data to Excel, this works fine
ExcelWbDi->Save(0);
ExcelWbDi->Close(OleVariant(false),
OleVariant(AnsiString("")),
OleVariant(Unassigned),
0);
ExcelWsDi->Disconnect();
ExcelWbDi->Disconnect();
ExcelApp->Quit();
}
catch(Exception *e)
{
MessageDlg("Error", mtError, TMsgDlgButtons() << mbYes, 0);
Abort;
}
...
=== End of ExApp.cpp ===
Thanks for any help.
Martin