node-activex icon indicating copy to clipboard operation
node-activex copied to clipboard

Excel application stays in memory

Open OSjoerdWie opened this issue 4 years ago • 7 comments

An "Excel.Application" Object leaves a process running after quitting the application and releasing the object: ` const winax = require("winax");

const excelApplication = new winax.Object("Excel.Application", { activate: false }); excelApplication.Quit() winax.release(excelApplication) ` winax: 1.19.0 Node: 12.18.0 Windows: Windows 10 Excel: Office 365 2007 (13006.20002 Click-to-Run)

OSjoerdWie avatar Jun 11 '20 11:06 OSjoerdWie

@OSjoerdWie, After the winax.release, I can see the application in the Background Process of Task Manager - but after a few minutes it disappears !! Does it persist in your case?

Ajitweb avatar Jul 09 '20 11:07 Ajitweb

I ran the test code about four hours ago and the instance of Excel is still present.

On Thu, Jul 9, 2020 at 1:05 PM Ajitweb [email protected] wrote:

@OSjoerdWie https://github.com/OSjoerdWie, After the winax.release, I can see the application in the Background Process of Task Manager - but after a few minutes it disappears !! Does it persist in your case?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/durs/node-activex/issues/78#issuecomment-656062653, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEYLJZ7BK4JPXUQGOLLOUYTR2WP6TANCNFSM4N3KYL4Q .

OSjoerdWie avatar Jul 13 '20 10:07 OSjoerdWie

Wish there was a method - for eg - excelApplication.getpid() which will return the pid of the excel instance Then all that has to be done is a - process.kill(pid) from node

I don't know if it is possible - but maybe you can to create a xlsm file in which you can create a macro to return the PID

Look at this

Ajitweb avatar Jul 14 '20 13:07 Ajitweb

I do have a solution for that in node already (browse existing instances of Excel using tasklist, get the matching one using and then perform taskkill). But still... this is a work around on top of the module you would expect to handle it for you.

OSjoerdWie avatar Jul 14 '20 14:07 OSjoerdWie

huhuime's killHwnd function seems to work for me:

[Question] How to get the process ID of the new activeXObject ? #53

kenklin avatar May 08 '22 17:05 kenklin

The function below works for me. Do not forget change EXCEL.EXE to your own application name, and run wmic process where name='EXCEL.EXE' get processid,commandline /format:list to check the key words.

import { taskkill } from 'taskkill'
import { spawnSync } from 'child_process'

function GetExcelProcessId() {
  const cmd = `wmic process where name='EXCEL.EXE' get processid,commandline /format:list`
  const result = spawnSync('cmd', ['/c', cmd], { encoding: 'utf8' })
  let pids = result.stdout.replace(/\r\r/g, '').split('\n\n').filter(line => line.includes('/automation -Embedding')).map(line => line.match(/ProcessId=(\d+)/)[1])
  if (pids.length) return pids[0]
  else return 0
}

// 用法:
taskkill(GetExcelProcessId())

Riant avatar Jun 24 '24 08:06 Riant

下面是使用 Get-CimInstance 的版本,ProcessId 的解析更容易解析一些,不过需要基于 windows 10 的 PowerShell 运行:

Get-CimInstance Win32_Process | Where-Object {$_.Name -eq "EXCEL.EXE"} | Select-Object ProcessId,CommandLine

Riant avatar Jun 25 '24 01:06 Riant