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

后端 未结 2 910
忘了有多久
忘了有多久 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 09:54

    You can try to set the focus in one of those windows and send the keys Alt F4 to Close those processes

    http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

    there are several ways to get handles to the windows of the processes.

    Good luck

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题