我需要使用powershell脚本从任务栏关闭word/excel/powerpoint文档。Stop-进程会终止进程,但不会保存所做的更改。我需要它来保存和关闭文档。我可以用下面的脚本为word做到这一点:
$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
对于powerpoint:
$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
https://stackoverflow.com/questions/38458819
复制联系客服