Microsoft KB Archive/266088

= BUG: Excel Does Not Shut Down After Calling the Quit Method When Automating from JScript =

Article ID: 266088

Article Last Modified on 5/15/2007

-

APPLIES TO


 * Microsoft JScript 5.0
 * Microsoft JScript 4.0
 * Microsoft JScript 3.0
 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Service Pack 1
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q266088



SYMPTOMS
When automating Microsoft Excel from Microsoft JScript, Excel stays in memory after calling the Quit method until you close Internet Explorer or navigate to another page.



CAUSE
JScript is holding on to a reference to Excel. Because there is a reference on Excel when you issue the Quit command, Excel does not shut down. JScript is a garbage collecting language, which means the engine cleans up after itself at a certain point, and not when you set the variables to NULL. When you shut down Internet Explorer or move to another page, the engine is destroyed. This behavior forces garbage collection and frees the reference to Excel.



RESOLUTION
To work around this problem, you can call the CollectGarbage method. This forces JScript's garbage collection to occur immediately, which releases the reference to Excel. The following code snippet illustrates how to use the CollectGarbage method:     var idTmr = &quot;&quot;;

function StartExcel { var oExcel;

oExcel = new ActiveXObject(&quot;Excel.Application&quot;); oExcel.Quit; oExcel = null; idTmr = window.setInterval(&quot;Cleanup;&quot;,1); }

function Cleanup { window.clearInterval(idTmr); CollectGarbage; }

   Notice that the CollectGarbage method is not called directly after Excel's Quit method. You need to give JScript a small amount of time before calling CollectGarbage. A timer is used in this example to show how to wait briefly before forcing garbage collection.

Another workaround to this problem is to use VBScript for Automation of Microsoft Excel. Unlike JScript, VBScript is not a garbage collecting language and, therefore, references are released when you set the variables to Nothing. Using VBScript, Excel shuts down immediately after calling the Quit method and releasing the variables. Please see the &quot;References&quot; section of this article for more information.

NOTE: The undocumented CollectGarbage method is not part of the ECMA-262 specification, and may not be available in future versions of the scripting engine. When you force the garbage collector to run by calling CollectGarbage, this may also negatively impact performance.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
  Start Notepad and paste the following code in the editor:   <INPUT type=&quot;button&quot; value=&quot;Automate Excel&quot; name=AutomateExcel onclick=&quot;StartExcel&quot;> <SCRIPT LANGUAGE=Javascript> function StartExcel { var oExcel;

oExcel = new ActiveXObject(&quot;Excel.Application&quot;); oExcel.Quit; oExcel = null; } </SCRIPT> </BODY> </HTML> </li> <li>Save the file as JScriptTest.HTM, and then exit Notepad.</li> <li>Double-click the JScriptTest.HTM file to load the file in Internet Explorer.</li> <li>Start the Windows Task Manager.</li> <li>Click the Automate Excel button on the Web page in Internet Explorer. Examine the Windows Task Manager, and note that Excel starts and stays in memory.</li> <li>Navigate to another page or exit Internet Explorer. Note that Excel quits and no longer appears in Windows Task Manager.</li></ol>

<div class="references_section">