使用Powershellclosuresexcel应用程序

我正在从PowerShell的工作簿中启动macros(以自动化一个进程)。 下面的PowerShell打开Excel的工作簿,并运行macros而不显示过程。

问题是,即使我没有看到macros运行,从macros生成的Excel的新实例仍然是打开的。

# start Excel $excel = New-Object -comobject Excel.Application #open file $FilePath = 'C:\file\Book1.xlsm' $workbook = $excel.Workbooks.Open($FilePath) #access the Application object and run a macro $app = $excel.Application $app.Run("macro") #close excel [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) Start-Sleep 1 'Excel processes: {0}' -f @(Get-Process excel -ea 0).Count Remove-Variable $excel exit $LASTEXITCODE 

excel文件在任务pipe理器中仍作为一个进程出现,占用内存空间。

如何让powershell完全closures通过macros打开的excel应用程序的实例?

在这里输入图像说明

任何帮助非常感谢!

尝试在释放COM对象之前使用Quit方法,如下所示:

 $excel.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) Remove-Variable excel 

你可以添加到你的PS代码:

 kill -processname excel 

这将closures所有打开的Excel实例