Getting Already Active Instance of Excel in MS JScript

816 views Asked by At

I'm struggling a bit trying to translate some C# code I wrote a while back into JScript. In the long run I need to open an Excel Workbook, or (if it's already open) bring it to the front. Unlike C#, JScript relies on a shoddy ActiveXObject to get a handle on the Excel application:

var Excel = new ActiveXObject("Excel.Application");

Once you've grabbed the object the API is pretty similar across all languages that the MSDN applies to. However, using the ActiveXObject seems to create an entirely new instance of Excel.

If I were to use the following code:

var Excel = new ActiveXObject("Excel.Application");
alert(Excel.ActiveSheet === null); // assume alert works as it does in Javascript

and Excel were already open, the alert would come back as true. Furthermore, any variables set with new ActiveXObject("Excel.Application") that haven't been picked up by garbage collection cause a new EXCEL.EXE process to appear:

enter image description here

Needless to say, all of this keeps me from checking if a workbook is already open, and subsequently causes an ugly "would you like to open this read-only" warning to appear. Is there any way around this?

1

There are 1 answers

11
Richard Deeming On BEST ANSWER

According to MSDN, you can use the GetObject function from JScript.

var Excel = GetObject(null, "Excel.Application");