none
Know in C# when a "Compared" word document is closed RRS feed

  • Question

  • Hi

    I need to know in C# (office interop) when a *compared* word document is closed by the user.

    I've had success for normal documents using DocumentEvents2_Event.Close to detect when a Word document has closed, but it does not fire for a document generated from _Document.Compare(..). I'm guessing the Compare document is a special case as when it is compared it displays three views; the compared document and source & revised documents (which is what the client wants to see).

    I'm using Word 2013 if that helps.

    Here is my simplified working:

    var wordApp = new Word.ApplicationClass();
    wordApp.Visible = false;
    wordApp.WindowState = WdWindowState.wdWindowStateMinimize;
    
    var doc = wordApp.Documents.Open(
    		FileName: "source.docx",
    		Visible: true,
    		ReadOnly: true);
    
    doc.TrackRevisions = true;
    doc.ShowRevisions = true;
    doc.PrintRevisions = true;
    
    doc.Compare("revised.docx");
    
    var compareDoc = wordApp.ActiveDocument;
    
    object oMissing = System.Reflection.Missing.Value;
    object oFalse = false;
    ((Word._Document)doc).Close(oFalse, oMissing, oMissing);
    
    compareDoc.SaveAs("compared.docx", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault);
    
    wordApp.NormalTemplate.Saved = true; // prevent word from trying to save Normal.dot
    
    wordApp.Visible = true;
    wordApp.WindowState = WdWindowState.wdWindowStateMaximize;
    
    ((Microsoft.Office.Interop.Word.DocumentEvents2_Event)compareDoc).Close += OnDocClose; // OnDocClose doesn't fire when document closes!

    Thanks


    • Edited by stevek5432 Wednesday, August 20, 2014 2:30 AM
    Wednesday, August 20, 2014 2:29 AM

Answers

  • Hi Steve

    I did some testing in VBA (that's always simpler and faster when checking theories and whether Word will or will not something) and I see that...

    If you compare two documents that are saved to file, comparing in a new document, closing that document DOES trigger the DocumentBeforeClose event.

    Note that I set the event up at the APPLICATION level, not at the document level. I don't think you can make that association at the document level outside of VBA or VSTO. So you might try the application-level event?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by stevek5432 Sunday, August 24, 2014 11:10 PM
    Sunday, August 24, 2014 12:13 PM
    Moderator

All replies

  • Try to declare the source object (compareDoc) which should fire events at the global scope. It may be swiped out by the garbage collector before you get the event. In that case the event will not be received.
    Wednesday, August 20, 2014 10:50 AM
  • Thanks Eugene, I thought that might fix it. I declared a static Document variable and set it to the ActiveDocument after the compare, but unfortunately the event still didn't fire.

    I added an application quit event handler to the local wordApp variable and it works fine.

    eg

    ((Microsoft.Office.Interop.Word.ApplicationEvents4_Event)wordApp).Quit += OnAppClose; // Works fine

      

    Is there any other way I can know when the compared document is closed?

    • Edited by stevek5432 Thursday, August 21, 2014 6:05 AM
    Thursday, August 21, 2014 6:04 AM
  • Hi Steve

    I did some testing in VBA (that's always simpler and faster when checking theories and whether Word will or will not something) and I see that...

    If you compare two documents that are saved to file, comparing in a new document, closing that document DOES trigger the DocumentBeforeClose event.

    Note that I set the event up at the APPLICATION level, not at the document level. I don't think you can make that association at the document level outside of VBA or VSTO. So you might try the application-level event?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by stevek5432 Sunday, August 24, 2014 11:10 PM
    Sunday, August 24, 2014 12:13 PM
    Moderator
  • Hi Cindy,

    Thanks for looking at this for me. The DocumentBeforeClose event does indeed work so I will use that.

    Thanks again,

    Steve

    Sunday, August 24, 2014 11:10 PM