Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MFC: Excel Automation Can't Quit from Excel. Please help...

114 views
Skip to first unread message

Landon

unread,
Sep 3, 2008, 5:48:00 AM9/3/08
to
Hi I use MFC Visual C++ 4.2.

I am automating Excel from my application.

My problem is I cannot quit the Excel after the automation process finished!

I checked it still exists on the Process lists and the most DANGEROUS is
everytime the automation function run, it create other Excel and so on. So it
must be out of memory soon.

I have used the app.Quit() method like all the people used but it DOES NOT
work.

What should I do?

Thank you.

Joseph M. Newcomer

unread,
Sep 3, 2008, 8:14:44 AM9/3/08
to
I happened to answer this on the office.developer.vba newsgroup. You most likely have
neglected to Release() the automation object.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Ajay Kalra

unread,
Sep 3, 2008, 10:13:36 AM9/3/08
to
Its very likely that you didnt release the object as Joe suggested.
Use CComPtr and its variants which will release the object when the
obejct goes out of scope.

--
Ajay

Joseph M. Newcomer

unread,
Sep 3, 2008, 12:53:26 PM9/3/08
to
Which, unfortunately, only works if the object is scope-controlled. If it is a class
member in some class, this won't do anything until the view, document, or whatever is
closed.

Also, it is not clear if subsequent invocations actualy launch a new version of Excel or
just tack onto the existing executable by putting up another top-level window. In
PowerPoint, I found that Quit() would quit all of PowerPoint, so if I had the sequence

Run My Indexing Program
Indexing Program Launches PowerPoint
Indexing Program finishes processing
Indexing Program Quits PowerPoint

it worked fine, but if I did

Run PowerPoint
Bring up six presentations
Run my indexing program
Indexing program opens document in PowerPoint
Indexing program finishes processing
Indexing program Quits PowerPoint
'
it actually closed the whole of PowerPoint, including all six open presentations.

Furthermore, I decided that if I had already opened the document in PowerPoint and just
wanted to index it after doing some edits, I did not want to actually close it. So my
code now does

If powerpoint is running, set the powerpoint-was-running flag
If the presentation is already open, set the presentatation-already-open flag
Open the document (which, if it is already open, does nothing and returns success)
index it
If the document was not already open, close the document
If powerpoint was not already running, quit powerpoint

Testing it in isolation may give the illusion that Quit() is the right approach, but as I
discovered, it isn't really the correct solution to the problem.

The code to handle this can be found in my PowerPoint Indexer program.
joe


On Wed, 3 Sep 2008 07:13:36 -0700 (PDT), Ajay Kalra <ajay...@yahoo.com> wrote:

>Its very likely that you didnt release the object as Joe suggested.
>Use CComPtr and its variants which will release the object when the
>obejct goes out of scope.

Ajay Kalra

unread,
Sep 3, 2008, 1:25:57 PM9/3/08
to
On Sep 3, 12:53 pm, Joseph M. Newcomer <newco...@flounder.com> wrote:
> Which, unfortunately, only works if the object is scope-controlled.  If it is a class
> member in some class, this won't do anything until the view, document, or whatever is
> closed.

Certainly. But using CCom* is very helpful in most cases. Even if you
use it in the scenario described above, you dont loose anything
compared to not using it.

--
Ajay

Joseph M. Newcomer

unread,
Sep 3, 2008, 3:02:48 PM9/3/08
to
I don't recall what my code actually does use at this point.
joe

Landon

unread,
Sep 3, 2008, 8:34:01 PM9/3/08
to
Yes, I also have tried to app.Release() after app.Quit() but it did not work
Joe, it threw a COleException.

So my code is like this:
// Release dispatch pointers.
range.ReleaseDispatch();
sheet.ReleaseDispatch();

sheets.ReleaseDispatch();

book.SetSaved( TRUE );
book.ReleaseDispatch();
book.Close( covTrue, covOptional, covOptional );

books.ReleaseDispatch();
app.Quit();
app.Release(); //threw COleException HERE.

1. Can you please look what do I miss there? If it is possible, can you
please give me the solution by adding / fixing my code above?

I have searched everywhere but I only found the solution in .NET and
JScript. They are totally different with MFC because they have been MANAGED
code so I don't know what to do.

2. How to apply the CComPtr to my code above? Can you please give me the
example of CComPtr using my code above?

3. What is the difference between Release() and ReleaseDispatch()?

Thank you very much.

Joseph M. Newcomer

unread,
Sep 3, 2008, 9:05:10 PM9/3/08
to
You'd have to download my PowerPoint Indexer and take a look. I'm basically in packing
mode tonight, will be leaving tomorrow.
joe

Mikel

unread,
Sep 4, 2008, 3:49:55 AM9/4/08
to
On 4 sep, 02:34, Landon <Lan...@discussions.microsoft.com> wrote:
> Yes, I also have tried to app.Release() after app.Quit() but it did not work
> Joe, it threw a COleException.
>
> So my code is like this:
>                 // Release dispatch pointers.
>                 range.ReleaseDispatch();
>                 sheet.ReleaseDispatch();
>
>                 sheets.ReleaseDispatch();
>
>                 book.SetSaved( TRUE );
>                 book.ReleaseDispatch();
>                 book.Close( covTrue, covOptional, covOptional );
>
>                 books.ReleaseDispatch();
>                 app.Quit();
>                 app.Release();     //threw COleException HERE.

In some code I have around I use app.ReleaseDispatch(); and it seems
to work...

0 new messages