1

I wrote a WPF/C# application that generates .docx (MS-Word) documents. I want the user to be able to edit the generated document and return to my WPF application.

Sadly, WYSIWYG editing of a .docx document can only be done with Word, so I had to start MS-Word (Word 2016) with my generated document from within my application. Now I am searching for a safe way to know when the user has finished editing the Word document. I assume that the user closes that instance of Word with my document, once he/she has finished editing. I need to "wait for Word" so I can continue to process that Word document. (The user can later choose to send the document as as email etc. pp. ). I am thinking about watching the file handles on my Word documents, but i find it too "hackish".

I wonder if anyone knows a safe and clean way to implement this.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Peter
  • 121
  • 6
  • probably not good enough to see if the winword process is no longer running? – kenny May 24 '18 at 14:31
  • @kenny can you watch out for a specific process? In other words, can i watch out for the word process i started? The user might have opened several word instances which are not related to me. – Peter May 24 '18 at 14:34
  • 1
    @Peter yes, [here](https://stackoverflow.com/a/3147920/9453080) – Ivan García Topete May 24 '18 at 14:39
  • or maybe you want something more like this https://stackoverflow.com/a/262291/3225 I don't think you can look for the one you started. – kenny May 24 '18 at 14:43
  • 1
    You cannot know which instance of Word "belongs" to you - this is all implementation-specific. MS can change the process creation policy any time and open documents, for instance, all in one process or group them arbitrarily. So I suspect, there's no real safe and clean way. – armenm May 24 '18 at 14:43
  • If your application is using the Interop, monitor the application's events. There's one that triggers when the user starts the Close process (DocumentBeforeClose), for example. – Cindy Meister May 24 '18 at 14:43
  • _so I had to start MS-Word_, so you started it as a `Process`? With `Process.Start`? Can you hook the `Exited` event? – Matt Burland May 24 '18 at 14:47
  • 1
    But ultimately, it's probably safest to just have the user tell you they are done. By clicking a button that says they are done. – Matt Burland May 24 '18 at 14:49
  • @CindyMeister okay, but how do i know the user has close the word instance i started and not some other instance? – Peter May 24 '18 at 14:55
  • @MattBurland yes, a done button might be the only way to be sure, from what i read here. – Peter May 24 '18 at 14:56
  • 2
    If *you* create the Word instance (`new Word.Application`, for example) then you can also monitor the `Quit` event of that application object. Note, however, that if the user does *not* quit it, but continues to work with it (opening other documents, etc.) you won't get that event immediately. OTOH you may not need to - as soon as the document is closed you can *release* that COM object (application = null;) and garbage collect it - it will then be just like any Word application as far as the user and Windows is concerned. – Cindy Meister May 24 '18 at 15:11
  • @CindyMeister thanks cindy, i will try your solution to this problem. – Peter May 25 '18 at 08:46

0 Answers0