none
DocumentChange Event at closing Word Document RRS feed

  • Question

  • Hi,

    I have a problem with the DocumentChange event for Word. For what i know, this event fires when the active document changes (not when the contents of the document change).

    This link says https://msdn.microsoft.com/en-us/library/office/ff822189.aspx

    "Occurs when a new document is created, when an existing document is opened, or when another document is made the active document."

    But i'm seeing that the event fires after closing the document, WHY???. At that moment the Application.ActiveDocument is closed. It throws me an COMException. I have to ask if the document is closed? its that OK? this is a Bug?

    Thanks

    Guillermo

    Thursday, September 1, 2016 2:37 PM

Answers

  • Hi,

    When you close the activedocument, you are actually inactivating this document, it should be a kind of changes, so the DocumentChange event fires again.

    >>At that moment the Application.ActiveDocument is closed. It throws me an COMException

    What action do you actually perform in the DocumentChange event?

    Are you using Application.ActiveDocument.Close() to close the document?

    What exception do you get after you close the document?

    According to my test, I think we couldn’t close or save ActiveDocument in DocumentChange event, because after this current document is closed, this document is inactivated, this would fire another DocumentChange event and after closing all the ActiveDocument, there is no ActiveDocument in the application, it would throw an exception: This command is not available because no document is open.

    I think that's why we need to check if the ActiveDocument closed to stop the event.

    I suggest you visit  Application.DocumentChange Event (Word) to check the sample code, it works well because after the event it wouldn’t fire it the second time.



    • Edited by Chenchen LiModerator Friday, September 2, 2016 5:37 AM
    • Marked as answer by Fiury Friday, September 2, 2016 1:55 PM
    Friday, September 2, 2016 5:37 AM
    Moderator

All replies

  • Hi,

    When you close the activedocument, you are actually inactivating this document, it should be a kind of changes, so the DocumentChange event fires again.

    >>At that moment the Application.ActiveDocument is closed. It throws me an COMException

    What action do you actually perform in the DocumentChange event?

    Are you using Application.ActiveDocument.Close() to close the document?

    What exception do you get after you close the document?

    According to my test, I think we couldn’t close or save ActiveDocument in DocumentChange event, because after this current document is closed, this document is inactivated, this would fire another DocumentChange event and after closing all the ActiveDocument, there is no ActiveDocument in the application, it would throw an exception: This command is not available because no document is open.

    I think that's why we need to check if the ActiveDocument closed to stop the event.

    I suggest you visit  Application.DocumentChange Event (Word) to check the sample code, it works well because after the event it wouldn’t fire it the second time.



    • Edited by Chenchen LiModerator Friday, September 2, 2016 5:37 AM
    • Marked as answer by Fiury Friday, September 2, 2016 1:55 PM
    Friday, September 2, 2016 5:37 AM
    Moderator
  • Hi,

    Thanks for the answer. Answering your questions

    What action do you actually perform in the DocumentChange event?
    Im getting the FullName, Applicaion.ActiveDocument.FullName, because if the active document change

    Are you using Application.ActiveDocument.Close() to close the document?
    No, only the user can close the document.

    What exception do you get after you close the document?
    System.Runtime.InteropServices.COMException{"This command is not available because no document is open."}

    I think i will check if the ActiveDocument is closed to stop the event then, thanks.


    Friday, September 2, 2016 1:21 PM