How to Quit or Close (not Kill) Word document (process)?

后端 未结 2 911
忘了有多久
忘了有多久 2021-01-18 09:16

In our company we are using Windows application to generate Word (2010) documents. Sometimes the document is not properly closed, so another application (yes, they still cal

2条回答
  •  星月不相逢
    2021-01-18 10:12

    You need to attach to the running Word instance as described here, and then close the document:

    $wd = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
    $wd.Documents | ? { $_.Name -eq 'some.docx' } | % {
      $_.Saved = $true
      $_.Close()
    }
    

    This must be run in the context of the user who started the application, and it will only attach to the instance started first (usually there's only one instance running anyway). You'd need to quit that instance first before you can attach to the instance started second.

提交回复
热议问题