Should an automated version of Excel close?

N

Norman Bullen

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?
 
P

Peter T

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
 
N

Norman Bullen

Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top