ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Should an automated version of Excel close? (https://www.excelbanter.com/excel-programming/418185-should-automated-version-excel-close.html)

Norman Bullen

Should an automated version of Excel close?
 
I'm writing an application in C++ which will read a couple of
complicated text files and create an Excel worksheet containing data
extracted from these files.

In the process of testing, I got to the point where the program:
- used CoCreateInstance() to create an instance of Excel and obtain
an IUnknown interface
- used QueryInterface() to obtain an IDispatch interface
- used Invoke() to set the Visible property to TRUE

At this point I can see an instance of Excel with no workbook visible on
the computer.

If I let this test version of the program go ahead and call Release() on
each of the interfaces which it has obtained, the instance of Excel
closes. This I take to be normal behavior of an automated application.

After adding code to get an IDispatch interface to the Workbooks object
and calling its Add method, the instance of Excel has a workbook
containing three worksheets, just as if it had been started by clicking
the Excel button.

Now, however if the program proceeds to call Release() on each of the
interfaces, Excel does not close. I have to manually close it by
clicking its close button.

Is this normal behavior? I'm concerned that I may have failed to release
an interface although I can't see what that interface might be. I know
that I have a IUnknown and IDispatch interfaces to the original Excel
object, an IDispatch interface to the Workbooks member, and an IDispatch
interface to the Workbook returned by the Add method. The program calls
Release() for each of these.

Is there anything else I should look for?
--
Norm

To reply, change domain to an adult feline.


Christian ASTOR

Should an automated version of Excel close?
 
On 7 oct, 16:35, Norman Bullen wrote:

After adding code to get an IDispatch interface to the Workbooks object
and calling its Add method, the instance of Excel has a workbook
containing three worksheets, just as if it had been started by clicking
the Excel button.

Now, however if the program proceeds to call Release() on each of the
interfaces, Excel does not close. I have to manually close it by
clicking its close button.


Have you called Close() then Quit() ?

Norman Bullen

Should an automated version of Excel close?
 
Peter T wrote:
Ensure any open workbooks are either saved (or the .Saved property is marked
saved) or use .Close(false).

Release any objects in the order created, eg worksheet, workbook etc.

Explicitly close the Excel instance with .Quit
Then release your reference to Excel.

Yes, what you describe is normal. Without an open workbook the instance will
close when all ref's to it are released, but with an workbook it'll stay
open unless you use "oXL.Quit".

Regards,
Peter T

That's what I was hoping to hear.
--
Norm

To reply, change domain to an adult feline.



All times are GMT +1. The time now is 03:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com