Windows 7 Gadget Does Not Free ActiveX Object

I am working on a Windows 7 gadget that is supposed to extract data from an excel document. The problem is that the Excel process will not be unloaded after I retrieve the data I need.

Here is the code that 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 all of this succeeds. All remote and null assignments are my attempt to release any references to COM objects, but I seem to be missing something. Is there a way to get the Excel process to unload?

+6
source share
2 answers

This is how Internet Explorer / JScript works - links are stored for a certain period of time until the garbage collector starts. The link should be garbage collected after a while if you set the variable to null . You can also force it to be collected using the (relatively undocumented) CollectGarbage() method available for 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 (10 ms here) before calling CollectGarbage() , otherwise, when you call the function, the variable may not have been marked for the collection yet.

Related support article http://support.microsoft.com/kb/266088

+6
source

I do not have Microsoft Office installed on my current computer, but I believe that you need to change Excel.Quit() to Excel.Application.Quit() .

This is because Excel is initialized as an ActiveX object, in particular Excel.Application , and not Excel itself.

0
source

Source: https://habr.com/ru/post/896249/


All Articles