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:
<HTML>
<BODY>
<INPUT type="button" value="Automate Excel" name=AutomateExcel onclick="StartExcel()">
<SCRIPT LANGUAGE=Javascript>
var idTmr = "";
function StartExcel() {
var oExcel;
oExcel = new ActiveXObject("Excel.Application");
oExcel.Quit();
oExcel = null;
idTmr = window.setInterval("Cleanup();",1);
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
</SCRIPT>
</BODY>
</HTML>
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 "References"
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.
REFERENCES
For
additional information a VBScript code sample that demonstrates Automation to
Excel, click the article number below to view the article in the Microsoft
Knowledge Base:
198703 HOWTO: Automating Excel From Client-Side VBScript
For more information on Office Automation, please
visit the Microsoft Office Development support site at: