我需要使用powershell脚本从任务栏关闭word/excel/powerpoint文档。停止进程会终止进程,但不会保存所做的更改。我需要它来保存并关闭文档。我可以使用以下脚本来完成此操作:
$wd = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
$wd.Documents | % { $_.Close() }
Get-Process | ?{$_.ProcessName -eq "WINWORD"} | Stop-Process
当我对 Excel 和 Powerpoint 执行相同操作时,我收到错误消息,并且更改未保存。 对于 Excel,我做了如下操作:
$excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
$excel.ActiveWorkbook | % { $_.Close() }
Get-Process | ?{$_.ProcessName -eq "EXCEL"} | Stop-Process
对于幻灯片:
$ppt= [Runtime.Interopservices.Marshal]::GetActiveObject('Powerpoint.Application')
$ppt.Presentations | % { $_.Close() }
Get-Process | ?{$_.ProcessName -eq "POWERPOINT"} | Stop-Process
显示的错误是:
+ $excel.ActiveWorkbook | % { $_.Close() }
+ ~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Close:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
最佳答案
如果需要,这将提示保存,然后关闭 Excel: (获取进程-名称“Excel”).CloseMainWindow
关于powershell - 如何使用powershell保存和停止MS Office进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38458819/