none
How to handle the Document.Close event RRS feed

  • Question

  • I'm using Office Automation to open Word Documents. I'd like to capture the close event so I can do a little processing. I haven't been able to cast to the DocumentEvents2_Event Close event because the interface doesn't seem to be available (it doesn't show up in intellisense and doesn't compile)

    I've found documentation on this method that is supposed to add the event, but have no idea how to proceed with it. 

    void add_Close(
    DocumentEvents2_CloseEventHandler param0
    )

    Can you give me some code that handles the Document.Close event?  Alternatively, perhaps I should be asking you why I shouldn't use it and what I should use instead.  ;^>

    Wednesday, June 6, 2012 10:57 PM

Answers

  • Hi shesamodiste,

    Thanks for posting in the MSDN Forum.

    This snippet will do it.

    ((Word.DocumentEvents2_Event)Application.ActiveDocument).Close += new Word.DocumentEvents2_CloseEventHandler(ThisDocument_Close); 

    Hae a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by shesamodiste Friday, June 8, 2012 6:07 PM
    • Unmarked as answer by shesamodiste Friday, June 8, 2012 6:59 PM
    • Marked as answer by shesamodiste Friday, June 8, 2012 7:01 PM
    • Unmarked as answer by shesamodiste Friday, June 8, 2012 8:22 PM
    • Marked as answer by shesamodiste Friday, June 8, 2012 8:49 PM
    Thursday, June 7, 2012 6:57 AM
    Moderator

All replies

  • Hi shesamodiste,

    Thanks for posting in the MSDN Forum.

    This snippet will do it.

    ((Word.DocumentEvents2_Event)Application.ActiveDocument).Close += new Word.DocumentEvents2_CloseEventHandler(ThisDocument_Close); 

    Hae a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by shesamodiste Friday, June 8, 2012 6:07 PM
    • Unmarked as answer by shesamodiste Friday, June 8, 2012 6:59 PM
    • Marked as answer by shesamodiste Friday, June 8, 2012 7:01 PM
    • Unmarked as answer by shesamodiste Friday, June 8, 2012 8:22 PM
    • Marked as answer by shesamodiste Friday, June 8, 2012 8:49 PM
    Thursday, June 7, 2012 6:57 AM
    Moderator
  • Works fine until I run it:

    Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to event interface type 'Microsoft.Office.Interop.Word.DocumentEvents2_Event'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{CDF92E2E-DF5E-3CAF-B7AF-CE7E96EFA6CD}' failed due to the following error: 'No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))' and the COM component does not support the source interface with IID '{00020A02-0000-0000-C000-000000000046}'.

    ***** Later that same day.....

    This must be attached to the Active document (just like your snippet said). Can't attach it to a specific document, even if that document happens to be the active document. Doh!


    Friday, June 8, 2012 8:24 PM
  • I noticed that this example has no event args that get passed to handler.  how do you know what doc is closing and how to react?
    Thursday, September 29, 2016 2:49 PM