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

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.
0
landon (165)
9/3/2008 9:48:00 AM
vc.mfc 33608 articles. 0 followers. Follow

8 Replies
3931 Views

Similar Articles

[PageSpeed] 39

I happened to answer this on the office.developer.vba newsgroup.  You most likely have
neglected to Release() the automation object.
				joe

On Wed, 3 Sep 2008 02:48:00 -0700, Landon <Landon@discussions.microsoft.com> wrote:

>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 [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
9/3/2008 12:14:44 PM
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

On Sep 3, 5:48=A0am, Landon <Lan...@discussions.microsoft.com> wrote:
> 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 finish=
ed!
>
> 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. S=
o it
> must be out of memory soon.
>
> I have used the app.Quit() method like all the people used but it DOES NO=
T
> work.
>
> What should I do?
>
> Thank you.

0
ajaykalra (6842)
9/3/2008 2:13:36 PM
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 <ajaykalra@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.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
9/3/2008 4:53:26 PM
On Sep 3, 12:53=A0pm, Joseph M. Newcomer <newco...@flounder.com> wrote:
> Which, unfortunately, only works if the object is scope-controlled. =A0If=
 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



0
ajaykalra (6842)
9/3/2008 5:25:57 PM
I don't recall what my code actually does use at this point.
					joe
On Wed, 3 Sep 2008 10:25:57 -0700 (PDT), Ajay Kalra <ajaykalra@yahoo.com> wrote:

>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.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
9/3/2008 7:02:48 PM
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.
0
landon (165)
9/4/2008 12:34:01 AM
You'd have to download my PowerPoint Indexer and take a look.  I'm basically in packing
mode tonight, will be leaving tomorrow.
				joe

On Wed, 3 Sep 2008 17:34:01 -0700, Landon <Landon@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.
>
>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 [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
9/4/2008 1:05:10 AM
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 w=
ork
> Joe, it threw a COleException.
>
> So my code is like this:
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // Release dispatch pointers.
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 range.ReleaseDispatch();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 sheet.ReleaseDispatch();
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 sheets.ReleaseDispatch();
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 book.SetSaved( TRUE );
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 book.ReleaseDispatch();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 book.Close( covTrue, covOptional, covOpti=
onal );
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 books.ReleaseDispatch();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 app.Quit();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 app.Release(); =A0 =A0 //threw COleExcept=
ion HERE.

In some code I have around I use app.ReleaseDispatch(); and it seems
to work...
0
mikel.luri (90)
9/4/2008 7:49:55 AM
Reply:

Similar Artilces:

Very annoying behaviour of Excel 2003
Each time I am opening Excel, it is installing some component. This takes several minutes, and becomes very annoying. I recently had to reinstall XP and Office from the distribution CDs. I have the problem since then. It certainly has something to do with the install. Does anyone on this forum ever experienced such a behaviour. I am running Office 2003 on XP SP3. have you tried doing a repair in the help menu? -- Gary Keramidas Excel 2003 "Jean" <Jean@discussions.microsoft.com> wrote in message news:020549D4-8070-46ED-8768-821EECD87C88@microsof...

My hide sheet option in excel is not working
My hide sheet option in Excel is not working. It is grayed out so I can not toggle nor use it to hide or unhide sheets. Security is at Medium - would appreciate any insight into how to fix this. Is the sheet protected? You cannot hide a protected sheet. -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "arleen" <arleen @discussions.microsoft.com> wrote in message news:D028B90F-6DDB-4DE6-9F5C-22C137FBE927@microsoft.com... > My hide sheet option in Excel is not working. It is grayed out > so I can not > toggle n...

How do I get excel to accept (c) as text and not change to copyri.
How do I enter the text (c) in Excel without having it changed into the copyright symbol? Hi Daffyd, Try: Tools | Autocorrect | Select (c) | Delete | OK --- Regards, Norman "daffyd" <daffyd@discussions.microsoft.com> wrote in message news:8CCC3C1A-6F19-4F62-B934-8A71F236A4FD@microsoft.com... > How do I enter the text (c) in Excel without having it changed into the > copyright symbol? Go into the Tools Menu. Look for AutoCorrect. In the bottom half of the AutoCorrect Tab, look at the list for Replace text as you type. Delete the entry for (c). tj "da...

Setting series values on Excel Chart
Hi I'm struggling very hard in getting beyond this error when trying to use VB to generate a chart from data previously inserted into the sheet. Unable to set the Values property of the Series class. The code works absolutely fine in XL2007 but I am having to port it back to 2003 and the inference is that its empty values that cause it to hiccup - something I cannot avoid. My code is: 1. Retrieve selected data from database and place it at the top of the sheet 2. Add The Chart 3. Iterate through the data (amount can be variable depending upon user inputs) For j as i...

Excel template that lists each month's meetings w/ some detail
I'm looking for an Excel template that lists every month of the year with room to list weekly, monthly and quarterly meetings, including some detail for each meeting. Probably would need to be landscape oriented. The main intent is to give the person using it an idea of upcoming meetings, the intent of each and what prep is required. ...

How do I make a 'box and whisker plot' graph in Microsoft Excel?
I am in Year 12 and am doing a stats assignment. The project states that i MUST insert a box and whisker plot for some data, but i don't know how to do this. Does anybody know? Hi, Here are some sites on the topic of stats charting not just box and whisker. http://peltiertech.com/Excel/Charts/statscharts.html http://www.processtrends.com/toc_statistical_charts.htm http://tushar-mehta.com/publish_train/data_analysis/16.htm http://people.stfx.ca/bliengme/ExcelTips.htm Cheers Andy -- Andy Pope, Microsoft MVP - Excel http://www.andypope.info "Year 12 student who needs help" &...

I've got a formula for stones and pounds
Hello, I've got a column that displays a series of weights in stones & pounds (both in the one cell). How do I get a chart to recognise and display in the same format? Regards, mikelenno What is the format of the cells with stone & pounds It is best to enter such values a 10 3/14 and format the cells with # ??/14 best wishes from one who is INT(275,14) stone & MOD(250,14) lbs -- Bernard V Liengme www.stfx.ca/people/bliengme remove caps from email "mikelenno" <mikelenno@discussions.microsoft.com> wrote in message news:5AD608FA-5367-4CED-AD76-1F54228EFDBA...

web address help
whats the web address for outlook so i can sign on? I have it stored on my home computer but dont have it here thanks Outlook doesn't have a web address unless you use an Exchange server. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com <anonymous@discussions.microsoft.com> wrote in message news:64da01c3e61b$58282fc0$a401280a@phx.gbl... > whats the web address for outlook...

HELP!! Viewing email images
I have just recently switched to using Outlook rather than Outlook Express as my email program. I now find that any image attachments (.jpg, .bmp etc.) I receive are not visible onscreen as they used to be in Outlook Express. I have to click on each attachment icon to view... It's annoying, especially if I get a series of pics...I have to manually ope each one... Does anyone know how to set Outlook so that it shows me these images onscreen??? Many Thanks, Darren. ...

help!! smtp protocol error occurred
exchange 2000 srv/windows 2k srv smtp protocol error occurred when trying to send mails. receives fine. Still waiting for some relavent information so we can help you solve your problem. >-----Original Message----- >exchange 2000 srv/windows 2k srv >smtp protocol error occurred when trying to send mails. >receives fine. > > >. > I had that problem, I telneted into the servers that the error occurred and found I was on a blacklist. Had to sumit to an open relay test, and bam, no more problems. ...

help installing exchange 2000 in one of 3 sites
I have a domain cotoso.com installed and configured as AD for 3 companies interlinked together. and + a total of 3 DC in 3 different sites that are connected by VPN. i have exchange installed in each site connected on the same domain contoso.Local(+ E-mail -->contoso.com) + hosting their own companie domain name such as x.com or y.co or z.com for each company. I have a routing link configured + 3 administrative groups in exchange system manger for each company. one DC in one site that also has exchange 2000 installed on it has to be changed because of poor performance. now i'm ...

Help !!!!
I was migration my Exchange 2000 to 2003 I do my forest prep and domain prep But i can't create mailboxes ??? Why??? some documentation please... What actually happens? What error do you get? -- Neil Hobson Exchange MVP For Exchange news, links, and tips, check: http://www.msexchangeblog.com "A.dian�" <Adian@discussions.microsoft.com> wrote in message news:31CB593F-F4DD-4CAB-9F4E-C2131BF7CF95@microsoft.com... > I was migration my Exchange 2000 to 2003 I do my forest prep and domain prep > But i can't create mailboxes ??? > Why??? > some documentation...

Data requires decimal point problem. Pls help.
Hi, I'm running Excel 2000 on a Win98 machine. When I enter a number in a cell Excel is now ignoring zeros on the right hand side. For example, if I type 100 <CR> the number 1 appears in the cell. Setting Format Options to two decimal places does not fix this. In that case I will get 1.00. Also, if that cell is used to multiply another the contents of another cell the result is just multiplied by 1. In order for me to get 100 in the cell I must type 100 and a decimal point. TIA, Joe Hi Joe try the following - goto 'Tools - Options - Edit' - uncheck 'fixed decimals...

why do changes only save locally on networked excel file?
why do changes only save locally on networked excel file? ...

Excel problem #3
I am attaching an excel file where i have a problem In the file are 2 sheets, Main & second I want to get data from second sheet to the main sheet by a formula by which the amount in the total column will be posted in the second sheet falling under various dates. I have done for 6 sept 2003 by way of example I do not know any formula by which i can do this automatically Please help me Attachment filename: example.xls Download attachment: http://www.excelforum.com/attachment.php?postid=444742 --- Message posted from http://www.ExcelForum.com/ Hi one way: ...

Help ! formatting data to text
I am creating data in an Excel spreadsheet. I then want to get that data into a simple text email. I have some problems and questions... 1) how do I get the columns of data to line up evenly when I copy the data to email text ? Keep in mind I need to be in simple text format, not HTML or rich text. Every time I do this, all columns become chaos and are unreadable. 2) Is there a simple way to automate the creation of an email from an excel file ? this is less important to me. Thanks in advance WxMachine #1. I think it may have to do with what email client you use, too. I copy and ...

Excel formulation to automate values
Hi there, I have a little problem on arranging a small multi-worksheet excel here. Let me explain in a short way then give some details on it. I am trying to make an offers recordsheet which has two worksheets in it. One for products(and their base prices) and an offer sheet. I would like to use a bit dynamic data here. So when I type the name of the product excel would give me the price from other worksheet. in details; Prices worksheet is something like: A B 1 Product BasePrice 2 mouse 5 3 keyboard 8 4 ... And the ...

Removing hidden mailto:s from Excel?
I received a spreadsheet attachment yesterday and found a mailto:(my address) embedded in a cell under a text string. It was evident in a tooltip that appeared when cell was hovered-over. I told the sender who now says that many of her spreadsheets - used to handle group memberships - have such hidden addresses. What terms would she use to search for these pests, either in My Docs or on opening each sheet? ...

Using Excel in Mac office 2004 with Microsoft/Mac 2008:Analysis toolpack
Although I am disappointed with Office 2008 for Mac because of the absent of Analysis toolpack, I figured things out quite recently that I can still install my Mac office 2004 and use the excel package that contains the Analysis tool pack. How? ...... let me show you: 1. Insert your CD for Mac office 2004 2. When it open on your desk top, two options will be given to you to download the software (a) Office set-up assistant, and (b) Microsoft office 2004. 3. CHOOSE MICROSOFT OFFICE 2004 (Drag the icon onto your desk top) and it will start to run/download. 4. When done, this will appear as ...

Printing Word and excel documents via excel vba
I have a document that has some Excel and some word content i would like to use excel vba to print both a page from word and then a page from excel. I know how to select and open the word document, however i cant find any code to print page X from word. basically i need to: print page 1 of c:\xx\document1.doc print page1 of sheet1 of c:\yy\excelwkbk1.xls print page 2 of c:\xx\document1.doc print page2 of sheet1 of c:\yy\excelwkbk1.xls and so on it will require moving between word and excel - the excel bit i get, but getting back to excel from word, and printing out in word is slightly more...

force excel to run in only one process excel.exe
force excel to run in only one process excel.exe, excel seems to want to make multiple excel.exe loaded into memory which causes mysheet.copy to fail so i have to close out the xls, kill all running excel.exe, then run the xls again and the macro works. i only have to do this when i am actively debugging a lot. if i just run my macro w/o opening the vba debugger, i can run it as much as i want, but if i run with break points it crashes a lot because of multiple excel.exe loaded into memory. perhaps there is a way i can force excel to only load one excel.exe? Daniel, Are you using Automatio...

Using Excel spreadsheet as input to Access
Hello, I posted this in the New Users forum but only got one answer, so thought I'd try here as well. Like so many others, I am an Excel newbie. I was a mainframe COBO programmer in another life, but that was a few years back My manager would like me to write an app that will take tracking dat from an existing Excel spreadsheet (generated by our system) but onl use a select handful of columns as input to a new Access database tha I will create. I'm guessing that I can either a) create a new edited spreadsheet to b used as input to the Access database or b) use the Import wiza...

Excel not displaying German characters correctly.
This is a real puzzler. Has our entire office and IT department stumped. We have two machines (M1 / M2) which are equal spec/ operating system, applications, versions, etc... The only difference that we can see between the two machines is that one (M1) has Internet Explorer encoding set to "UTF-8" with "Automatic" selected. The other (M2) has "Western European ISO" with no automatic selected. Now, when the machines log into a web application and choose to download a German Excel file, M1 and M2 get the file correctly if they have their encoding settings...

MS Excel Paste Special>Paste All
Using MS Excel 2003 Paste Special> Paste All, doesnt quite paste the 'Column Widths'. Am I always required to do the two step dance, Paste Special> Column Widths and Paste Special>All. >>Am I always required to do the two step dance Yes<g>. -- Jim "Excel User" <Excel User@discussions.microsoft.com> wrote in message news:5D34ED9D-5BD6-43AD-8773-1EB7229309B2@microsoft.com... | Using MS Excel 2003 | | Paste Special> Paste All, doesnt quite paste the 'Column Widths'. Am I | always required to do the two step dance, Paste Special> ...

Newbe help with a lookup
Hi, I&#8217;m trying to make some modifications to a salary matrix and I&#8217;m having trouble with a lookup. In column A I have a series of numbers from 0 to 30 representing pay grade steps where 0 represents a new employee and 30 represents an employee having completed 30 years of service. In column B I have an annual salary for each of the corresponding pay grade steps. I want to take a new salary and find the closest salary in column B and return the corresponding step number. For example if the new salary is $73,415.13 and the matrix shows step 15 is 73,205 and step 16 is 73...