使用Powershell关闭Excel应用程序 [英] Close excel application using Powershell
问题描述
我正在从Powershell启动工作簿中的宏(以使过程自动化). Powershell中的以下内容打开了excel工作簿并在不可视化过程的情况下运行了宏.
I am initiating a macro in a workbook from powershell (to automate a process). The below in powershell opens the excel workbook and runs the macro without visualizing the process.
问题是,即使我看不到宏在运行,从宏生成的excel新实例仍处于打开状态.
The issue is even though I do not see the macro running, the new instance of excel generated from the macro is still open.
# 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文件仍然作为任务管理器中的一个进程出现,并占用了内存空间.
The excel file still comes up as a process in task manager and is taking up memory space.
我如何让Powershell完全关闭通过宏打开的excel应用程序的实例?
How do I have powershell completely close the instance of the excel application that opens through the macro?
任何帮助都将不胜感激!
Any help greatly appreciated!
推荐答案
在释放COM对象之前,尝试使用 Quit 方法,如下所示:
Try using Quit method before you release COM object, like this:
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
这篇关于使用Powershell关闭Excel应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!