0

Windows 7
PowerShell version 2.0

Get-WimObject Win32_Process … only returns one process instance of MS Word. I need to get all of the documents opened by MS Word and close 1 of N documents based on the name of the file being edited. The PowerShell script also needs to close the 1 of N documents cleanly so that the next time the document is open MS Word does not prompt the use which file to recover/open.

Ansgar Wiechers is wrong. This is not a duplicate. The link provided in the comment that it was duplicate was for excel, not word. On top of that the the link does not say how to cleanly stop 1 of N MS Word documents.

paulhr
  • 339
  • 4
  • 17
  • The problem you are going to run into is that you have to ask the process to close just that document cleanly. But it sounds like you are trying to do that to an existing application. So maybe with `Interop` you can attach to it, then call methods against the Word COM Object to find the document and then call `.close()` against it. But I don't think it'll be straightforward. – BenH Aug 22 '17 at 20:25
  • 1
    Your question is very much a duplicate. Whether it's Excel or Word is doesn't matter in the least. You need to attach to the already running instance of the program. The accepted answer to the other question shows how to do that and lists the relevant caveat: it works only when the instance is running in the same user context. Once you have a handle to the Word instance you can iterate over the `Documents` collection and work with the documents as if you had started the instance yourself via `New-Object -Com 'Word.Application'`. – Ansgar Wiechers Aug 23 '17 at 21:34
  • The question that Ansgar linked definitely answers the core issue which is attaching with Interop. Where `Win32_Process` doesn't work, attaching with Interop does. `Excel.Application` and `Word.Application` both work the same in this context. If you have further issues after reading up on working with Office as COM Object, then ask additional questions with where you ran into issues with your code. – BenH Aug 24 '17 at 16:39

0 Answers0