admin管理员组文章数量:1434959
I'm working on a Windows 7 gadget that needs to pull data from an excel document. The problem is, is that the Excel process won't unload after I've retrieved the data I need.
Here's the code I use in my initialization function:
var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = false;
Excel.DisplayAlerts = false;
var workbooks = Excel.Workbooks;
var workbook = workbooks.Open("\\\\SERVER\\Documents\\Sample.xlsx", 0, true);
var activesheet = workbook.ActiveSheet;
var cell = sheet.Cells(1, 1);
var value = cell.Value;
document.getElementById("content").innerHTML = value;
delete value;
value = null;
delete cell;
cell = null;
delete activesheet;
activesheet = null;
delete workbook;
workbook = null;
delete workbooks;
workbooks = null;
Excel.Quit();
delete Excel;
Excel = null;
This is all wrapped in a try-catch block and I can verify that it's all succeeding. All the deletes and null assignments are my attempt to release any references to the COM objects, but I seem to be missing something. Is there some way I can force the Excel process to unload?
I'm working on a Windows 7 gadget that needs to pull data from an excel document. The problem is, is that the Excel process won't unload after I've retrieved the data I need.
Here's the code I use in my initialization function:
var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = false;
Excel.DisplayAlerts = false;
var workbooks = Excel.Workbooks;
var workbook = workbooks.Open("\\\\SERVER\\Documents\\Sample.xlsx", 0, true);
var activesheet = workbook.ActiveSheet;
var cell = sheet.Cells(1, 1);
var value = cell.Value;
document.getElementById("content").innerHTML = value;
delete value;
value = null;
delete cell;
cell = null;
delete activesheet;
activesheet = null;
delete workbook;
workbook = null;
delete workbooks;
workbooks = null;
Excel.Quit();
delete Excel;
Excel = null;
This is all wrapped in a try-catch block and I can verify that it's all succeeding. All the deletes and null assignments are my attempt to release any references to the COM objects, but I seem to be missing something. Is there some way I can force the Excel process to unload?
Share Improve this question asked Aug 30, 2011 at 20:34 EclipseEclipse 45.6k20 gold badges116 silver badges172 bronze badges2 Answers
Reset to default 6 +50This is just how Internet Explorer/JScript works — references are held for a period of time until the garbage collector runs. The reference should be garbage collected after a while if you set the variable to null
. You can also force it to be collected by using the (relatively undocumented) CollectGarbage()
method available to JScript and IE:
var Excel = new ActiveXObject("Excel.Application");
//... blah ...
Excel.Quit();
Excel = null;
window.setTimeout(CollectGarbage, 10);
Note that you need to leave a small amount of time (10ms here) before calling CollectGarbage()
, otherwise when you call the function the variable may not have been marked for collection yet.
Related support article: http://support.microsoft./kb/266088
I don't have Microsoft Office installed on my current machine, but I believe you have to change Excel.Quit()
to Excel.Application.Quit()
.
This is because Excel is initialized as an ActiveX Object, specifically Excel.Application
, and not Excel
by itself.
本文标签: javascriptWindows 7 Gadget not releasing ActiveX objectStack Overflow
版权声明:本文标题:javascript - Windows 7 Gadget not releasing ActiveX object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745623206a2666812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论