MichaelDibble
Programmer
Hi
I've managed to write some code to save data to an Excel spreadsheet.
The problem is Excel doesn't close after this function has finished. Using Task manager Excel is still active.
I have found that if I comment out the Worksheet->Connect line, Excel closes properly. As soon as I connect to the worksheet, Excel fails to close.
I'm using the TExcelApplication, TExcelWorkbook, TExcelWorksheet components in C++ Builder 6.
Here is my code...
{
int lcid = LOCALE_USER_DEFAULT;
TVariant vTemplate = EmptyParam;
TVariant vFileName = FileName;
TVariant vSaveChanges = true;
try
{
try
{
ExcelApp->Connect();
}
catch(Exception &exception)
{
::MessageDlg("Excel may not be installed", mtError, TMsgDlgButtons() << mbYes, 0);
Abort;
}
// Set ExcelApp to invisible
ExcelApp->set_Visible(lcid, false);
// Create a new workbook
ExcelApp->Workbooks->Add(vTemplate, lcid);
WorkBook->ConnectTo(ExcelApp->ActiveWorkbook);
// Access the first worksheet
WorkSheet->ConnectTo(WorkBook->ActiveSheet);
// Worksheet access functions
// Disconnect the worksheet
WorkSheet->Disconnect();
// Save then close the WorkBook
WorkBook->Close(vSaveChanges, vFileName, TNoParam(), lcid);
WorkBook->Disconnect();
// Close Excel
ExcelApp->Quit();
ExcelApp->Disconnect();
}
catch(Exception &exception)
{
Application->ShowException(&exception);
WorkSheet->Disconnect();
WorkBook->Disconnect();
ExcelApp->Quit();
ExcelApp->Disconnect();
}
delete WorkSheet;
WorkSheet = NULL;
delete WorkBook;
WorkBook = NULL;
delete ExcelApp;
ExcelApp = NULL;
}
Thank you in advance for your help
Michael Dibble
I've managed to write some code to save data to an Excel spreadsheet.
The problem is Excel doesn't close after this function has finished. Using Task manager Excel is still active.
I have found that if I comment out the Worksheet->Connect line, Excel closes properly. As soon as I connect to the worksheet, Excel fails to close.
I'm using the TExcelApplication, TExcelWorkbook, TExcelWorksheet components in C++ Builder 6.
Here is my code...
{
int lcid = LOCALE_USER_DEFAULT;
TVariant vTemplate = EmptyParam;
TVariant vFileName = FileName;
TVariant vSaveChanges = true;
try
{
try
{
ExcelApp->Connect();
}
catch(Exception &exception)
{
::MessageDlg("Excel may not be installed", mtError, TMsgDlgButtons() << mbYes, 0);
Abort;
}
// Set ExcelApp to invisible
ExcelApp->set_Visible(lcid, false);
// Create a new workbook
ExcelApp->Workbooks->Add(vTemplate, lcid);
WorkBook->ConnectTo(ExcelApp->ActiveWorkbook);
// Access the first worksheet
WorkSheet->ConnectTo(WorkBook->ActiveSheet);
// Worksheet access functions
// Disconnect the worksheet
WorkSheet->Disconnect();
// Save then close the WorkBook
WorkBook->Close(vSaveChanges, vFileName, TNoParam(), lcid);
WorkBook->Disconnect();
// Close Excel
ExcelApp->Quit();
ExcelApp->Disconnect();
}
catch(Exception &exception)
{
Application->ShowException(&exception);
WorkSheet->Disconnect();
WorkBook->Disconnect();
ExcelApp->Quit();
ExcelApp->Disconnect();
}
delete WorkSheet;
WorkSheet = NULL;
delete WorkBook;
WorkBook = NULL;
delete ExcelApp;
ExcelApp = NULL;
}
Thank you in advance for your help
Michael Dibble