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
2202 Views

Similar Articles

[PageSpeed] 4

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 (15977)
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 (6840)
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 (15977)
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 (6840)
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 (15977)
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 (15977)
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:

A strange problem with MFC
Hi, Rencently I met a strange problem. The code like below: CWinThread* pThread = AfxBeginThread(ThreadFunc, (LPVOID)this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); if(pThread->m_hThread != NULL){ pThread->m_bAutodelete = FALSE; pThread->ResumeThread(); } After the statement "pThread->m_bAutodelete = FALSE" executed, I found the value of pThread->m_hThread is 0. While pThread->m_bAutodelete is still 1! The code section located in a win32 static library, with /Md option.The host application(exe) is a win32 console application, with /Md option. There...

pls help troubleshoot ms office...unable 2 configure ..so unable
-- mcp Where do you get stuck and in which version of Outlook? Account settings for the largest free email providers can be found here; http://www.howto-outlook.com/howto/accountsettings.htm BTW: Next time, use the body to specify your configuration and to clarify your request. -- Robert Sparnaaij [MVP-Outlook] Coauthor, Configuring Microsoft Outlook 2003 http://www.howto-outlook.com/ Outlook FAQ, HowTo, Downloads, Add-Ins and more http://www.msoutlook.info/ Real World Questions, Real World Answers ----- "Rambo" <Rambo@discussions.microsoft.com> w...

excel information
I am looking for information to see if it possible to change the color of a number in a cell if that numbers value is less than another number. Example if I post a three in cell a4 and it is a three and it is less that 4 can that three show up in red rather than black> hi i think you are talking about conditional formating. read up on conditional formating in excel help. also see this site. http://www.contextures.com/xlCondFormat01.html regards FSt1 "rddodge1" wrote: > I am looking for information to see if it possible to change the color of a > numbe...

return int value to OS in MFC App
Hi, Everybody. I have to make A MFC app to return int value to Operatiing System.. if i could use WinMain() function, this is not matter. but MFC does not reveal WinMain().... How can i solve this problem? Can Anybody help me with this problem? <flybluewing@bcline.com> wrote in message news:1189386249.923483.59250@y42g2000hsy.googlegroups.com... > Hi, Everybody. > > I have to make A MFC app to return int value to Operatiing System.. > > if i could use WinMain() function, this is not matter. > but MFC does not reveal WinMain().... > How can i solve this problem?...

sending excel
When I tried to send the program excel from one of my computers to my other computer by email. It was opening when all of a sudden, it said one of the components was missing. ms097.dll Can I not email the program excel. I own it and I was sending it to another computer. You can send Excel workbooks from one computer to another, but not the Excel program. Check the licence that came with your copy of Excel. You can use the CDs to install the program on one computer, and perhaps another copy can be installed on a laptop computer. mike wrote: > When I tried to send the program excel fr...

help urgent
i cant download attachment. system complain OE remove access to file. what is OE pls Tools-> Options-> tab Security-> option Do not allow attachments... Note that this is an Microsoft Office Outlook forum. Outlook Express is family of Internet Explorer and Outlook of the Office family. Here is the link for the right forum http://communities.microsoft.com/newsgroups/default.asp?icp=InternetExplorer Good Luck! -- Robert Sparnaaij [MVP-Outlook] www.howto-outlook.com Tips of the month: -Creating Signatures -Create an Office XP CD slipstreamed with Service Pack 3 ----- "ade&...

Excel if statement help
I have a valid, working excel formula that states: =IF(BL17>=1,1,0) However, I need it to say If bl17 (or bl19) are greater or equal to 1, than 1, else 0 The or bl19 is really the only new part. How can I add that in with correct syntax ? Just look in the help index for OR -- Don Guillett Microsoft MVP Excel SalesAid Software dguillett1@austin.rr.com <cegarbage@gmail.com> wrote in message news:df47d203-0fc7-4b73-ae96-662b900f835b@x41g2000hsb.googlegroups.com... >I have a valid, working excel formula that states: > > =IF(BL17>=1,1,0) > > However, I need it to ...

excel 2007 trendline equation
I'm used to see t trendline updating automatically when the source data change. Now this does not work any more. In the Q&A I read bout dynamic charts what does that mean ? Hi Erwin, I can't duplicate your problem. If my data changes the associated trendline changes. Are you adding the trendline via the Chart Tools, Layout command? Is Recalculation set to Automatic? -- Cheers, Shane Devenshire "Erwin_Goethals" wrote: > I'm used to see t trendline updating automatically when the source data change. > Now this does not work any more. > In the Q&A...

Unicode in MFC
Hi, I am new to MFC programming and I have a problem: when I try to write the caption of a button or something else in my own language(Farsi), in the design mode it shows the correct string but when I compile it it shows a '?' instead of every character. Can anyone please help me with that. > I am new to MFC programming and I have a problem: > when I try to write the caption of a button or something else in my > own language(Farsi), in the design mode it shows the correct string > but when I compile it it shows a '?' instead of every character. Can > anyone ple...

raisers edge/pivot tables in excel 2007
I use raisers edge 7.83, when i export a pivot table to excel 2007 it's not correct. It WAS correct in excel 2003. Anyone know of a fix for this? Unfortunately I do not know the answer to your question. However, i am looking for someone skilled and experienced with Raisers Edge for a role we currently have. So if you are experienced with Raisers Edge, like working with Data and are looking for a new role, then please do contact me asap. Thanks Mark Gibbons G & G Recruitment Solutions 0844 800 4984 -- Mark Gibbons ...

mfc and IE activeX
I'm using VS 2003 and trying to use Internet Explorer in my MFC application but encountering problems If I built a very simple test dialog-application and drop in the IE it works exactly as expected. However, when I create a similar dialog box in my MFC application and call the ...DoModal() nothing appears at all - not even the dialog box itself. It's as if my application does not want to use ActiveX components - I also tried a couple of other Microsoft ActiveX controls and they have the same problem. I must be missing something obvious - any one with any ideas will be much ...

Deleting Duplicates in Excel
I have put my Palm Pilot contacts in Excel. The fields are not all the same and there are many duplicates. Not all same names are exactly identical. I want to run a formula that will automatically delete duplicates if just the names (first and last) are the same. Some info in the rest of the lines may be different for various reasons. I don't care. Rather just have one entry for everyone. Does anyone know how to do this? In Excel-Data tab>Data Tools Command Group>Remove Duplicates. Select the column(s) you have duplicates in and want removed. "DColeman&q...

How do I convert .qpw files to excel format?
I have all my income tax files in .qpw format but no longer have that program to open them (computer change). I now have Excel and was wondering how I convert those .qpw files into Excel format? Thanks Try downloading and installing the WordPerfect 12 demo from www.corel.com. This includes Quattro Pro 12. A fully-functional 30-day trial is here: http://www.corel.com/servlet/Satellite?pagename=Corel3/Downloads/Trials I have Quattro Pro 11 and notice it will "Save as..." in "Excel 97" format. I just saved a Quattro Pro spreadsheet in that format and opened it in Exce...

HELP: Cannot retrieve my Outlook/Outlook Express messages from my Yahoo email account
In the past, I have always been able to use my Yahoo email account to download my emails from my Outlook/Outlook Express POP server. In other words, I could always use Yahoo to access my Outlook email without Outlook at all. Just go into an Internet cafe anywhere in the world and I can access my Outlook emails from the Yahoo email site. But lately, the Yahoo email kept giving an error message: "POP server does not support LAST command. You may only retrieve POP mail from this server using "get all messages". The Yahoo email webpage doesn't seem to allow me to input this ...

Excel Charting across Worksheets
Hi, I am trying to create a simple line chart using the same cell (C3 in this case) on four contiguous worksheets (Sheet1 through Sheet4) as my 4 data points. I have tried generating the chart using the chart wizard and setting Cell C3 on Sheet 1 as my data series. I then tried modifying that first data point (listed in the data series as 'Sheet1'!$C$3) to read 'Sheet1:Sheet4'!$C$3 (sort of like summing the same cell on different worksheets) but this gives me an error. If any one can show me how to do this either by giving me steps to follow while using the chart wiz...

Need Help with MFC!!
Hi all, need help esp. with Graphic part. When you right click on any drive on your machine and click properties, you get a tabbed dialog. I want to know how can I embed graphic image (pie chart) into my MFC Dialog? I have written code which displays the Disk Usage right now but don't know how to display that info as a PIE CHART!!!!!! besides what type of MFC Dialog application should I look fwd to build? I want exactly as the DRIVE properties. TIA -- ======== Regards Vai ======== Have you looked at the CDC::Pie method? joe On Wed, 12 May 2004 23:07:11 -0400, "Vai2000"...

Calendar control Excel
I have a calendar control on a userform in excel. I want to put th date from the calendar control into a cell on the spreadsheet. Tha ok, it lets me do one cell, when I try to click on a different cell o the spreadsheet, nothing happens. How can make the cells in th spreadsheet active so I can click on a different cell to use th calendar control to enter another date into a cell. : -- Message posted from http://www.ExcelForum.com Louis, Private Sub Worksheet_SelectionChange(ByVal Target As Range) Calendar1.LinkedCell = Target.Address End Sub Put this in the sheet module. -- Earl Ki...

Arabic/Farsi in Office 2004 (Word and Excel)
How do you setup Word 2004 and Excel 2004 to type right to left in arabic or farsi. I have enabled Farsi and arabic in the international pane of the System Preferences and I am able to type perfectly ok in TextEdit. However when I type Arabic or Farsi in Word 2004 the characters appear but they do not connect to one another as they do in TextEdit. Any ideas on how to fix this?? You can not type right to left in Office:Mac yet. This is certainly something to expect from the next version of Office:Mac... With Office:2004 dating from mid 2004, I can expect that the next Office might come out ...

outlook bug
error if we export a calendar to excel "Beim Importieren oder Exportieren ist ein Fehler aufgetreten und der Import/Export-Vorgang wurde abgebrochen. Der MAPI-Fehler 0X80004005 wurde gemeldet...." An error occurred while importing or exporting and the import/export was terminated. MAPI error 0X80004005 was reported. Please try to import/export later. other PC - same error no virusscanner - same error has anybody an idea Windows XP SP2 Outlook 2003 Franz Chernjak, you wrote on Fri, 23 Sep 2005 08:50:00 +0200: > An error occurred while importing or exporting and the im...

Excel 2000 files open with Excel 2010
I have just installed Office 2010 but have retained Office 2000. All my Excel 2000 files now open with Excel 2010. I've tried going into File Types and selecting the Excel 200 executable as the file to open XLS files, but they still open with Excel 2010. How can I return the default program for opening XLS files to C:\Program Files\Microsoft Office\Office\Excel.exe instead of using C:\Program Files\Microsoft Office\Office14\Excel.exe? I know I can open Excel 2000 and opening the files from there, but that is much more inconvenient. Any ideas? -- Ian -- To set a particular ver...

MFC to Console.
Hello Group. I need to convert the code of WiniNet Async Demo from microsoft samples from MFC to a console function.. Someone can help me with the changes.. The code is posted here Regards /* Microsoft Corporation Copyright 1999-2002 */ /******************************************************************** ProjectName : AsyncDemo Purpose : This sample demonstrates how to submit two WinInet requests, using InternentOpenUrl, asynchronously. Notes : This sample does not handle any authentication. To properly handle authentication, the functions that handle specific protocols (like Ht...

Am I stupid, or is it Excel???????
1. We have a list of numbers. 2. We need to calculate the average of the numbers 3. We need to know how many numbers in the series are equal to, or greater than the average. This works well in two stages ie. =Average then =countif......but if these are nested as a single function Excel returns a zero value. Are we completely stupid (sensible answers only), or have we missed the point of Excel completely. Also, using the wonderful expression builders, returns the same zero value. Winner is entitled to a lifetimes supply of Cranberry Juice. Difficult to say without your formula! -- Ki...

XML and MFC
Hello guys, What source of knowledge would you point me to if you were about to integrate XML with MFC? Of course if there's any Microsoft documentation anywhere, please post me a link to it. Thank you in advance! :-) Yea, I wanna know it too. Who would give a hand? > What source of knowledge would you point me to if you were about to > integrate XML with MFC? Of course if there's any Microsoft documentation > anywhere, please post me a link to it. After digging left and right, I remained with two main options: - MSXML - MS, proprietary, COM, with all the implications:...

Saving Excel workbook as web page
EXCEL 2002 doesn't let me save an entire workbook as a web page without adding spreadsheet functionality, which gives me the grid that I don't want and limits the viewable size. Any ideas? Is this a bug for which ther's a fix? Brian ...

help with adding... SUMIF formula?
I'm looking for a formula that will do the following: A1-A12 contains numbers. and I want a formula that will add up whatever number of cell I enter in B1 example: B1, I enter 6 the formula will add from A1 to A6 9 will add up A1-A9, etc... sumif formula maybe? if anyone can help I'd appreciate it. thanks! One way: =IF(B1>0,SUM(OFFSET(A1,0,0,B1,1)),"") In article <1163442974.688205.112410@h48g2000cwc.googlegroups.com>, ppaauull@gmail.com wrote: > I'm looking for a formula that will do the following: > > A1-A12 contains numbers. and I wan...