Excel COM Server connection does not close

I have an application written in Embarcadero C++ builder XE2 which uses the Excel Office COM server components that ship with the compiler.

It is a simple application that opens a spreadsheet and processes data from one of the spreadsheets.
I am using 3 components (TExcelWorksheet,TExcelWorkbook and TExcelApplication)
The application functions as it should but after the application has run, the task manager reveals that the Excel.exe *32 process is still running and this results in an erro when the application is subsequently run.

The destructor in the main form of my application contains the following code which I am using to close the Excel components but it would appear that this is not working.

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

I have also tried the following:
    ExcelWorksheet1->Disconnect();
    ExcelWorkbook1->Disconnect();
    ExcelApplication1->Disconnect();
    ExcelApplication1->Quit();

Is there a way to fix this ?
LVL 2
Roger AlcindorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zack BarresseCEOCommented:
I'm not fluent with C++, but wouldn't you want your logic like this...

Do stuff with Excel
Quit Excel
Disconnect objects


?

Regards,
Zack Barresse
0
Roger AlcindorAuthor Commented:
I have tried the following in the main form destructor and that doesn't fix the issue:

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

In C++ the main form destructor is where you place code to clean up and free any resources that have been used in the main form, in this case it is the Excel COM components that were used.
0
Roger AlcindorAuthor Commented:
I finally discovered a fix by calling the set_DisplayAlerts function prior to quitting the Excel application:

 ExcelApplication1->set_DisplayAlerts(TDefLCID(),0);
 ExcelApplication1->Quit();

This is surprising as I am opening the workbook as read-only.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roger AlcindorAuthor Commented:
I discovered the fix by myself.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.