powershell - 如何使用powershell保存和停止MS Office进程?

标签 powershell

我需要使用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/

相关文章:

git - 使用 Windows PowerShell 设置 .SSH key 时出错

powershell - 为什么要为 $ 赋值?崩溃Powershell?

java - Powershell 与 Batch - Java 命令行

powershell - 使用 WinRM 时出现 PSRemotingTransportException - IIS Web 应用程序部署

powershell - 根据创建时间搜索文件

powershell - 在新窗口中运行Powershell

powershell - 从 Powershell 中的字符串中去除所有非字母和数字?

powershell - 将参数传递给作业初始化脚本

powershell - 如何使用 Powershell 修改父作用域变量

powershell - Windows/Powershell 将程序版本获取到变量中