durs / node-activex

Node.JS Implementaion of ActiveXObject
MIT License
329 stars 62 forks source link

Q: How to obtain a running instance of Excel (Marshal.GetActiveObject?) #118

Closed kenklin closed 1 year ago

kenklin commented 1 year ago

I'm not familiar with Excel programming but have read https://learn.microsoft.com/en-us/office/vba/api/overview/excel and managed to use winax to launch a new instance of Excel with a supplied filename f and manipulate its worksheet's cells with code like this:

    application = new winax.Object("Excel.Application", {activate: true});

    const updateLinks = 0;
    const readOnly = false;
    workbook = application.Workbooks.Open(f, updateLinks, readOnly);

    <get worksheets and manipulate its cells>

My question is how to access an already running instance of Excel using winax? After some Googling, it seems I may need to somehow access Marshal.GetActiveObject ? If so, if you could point me to sample code using winax, I'd be grateful - thanks in advance.

durs commented 1 year ago

That's right, when using the activate option, the code is called:

CComPtr<IUnknown> unk;
hrcode = GetActiveObject(clsid, NULL, &unk);
if SUCCEEDED(hrcode) hrcode = unk->QueryInterface(&disp);
somanuell commented 1 year ago

To access an already running instance of Excel you must use the "getobject" option, not the "activate" one. Code sample:

var oApp = new winax.Object("Name of the File in the ROT", {getobject: true});

kenklin commented 1 year ago

@somanuell Thanks for the JS-side example! Closing.

kenklin commented 9 months ago

Aside - my Javascript code has been migrated to resides in an Excel Add-in.