none
Document Close event doesn't fire if document was created by shortcut on task panel RRS feed

  • Question

  • Hello, 

    It looks like I found a problem with Document.Close event. Here are steps to reproduce it:

    1. Launch Word and create and empty document.

    2. Start VBA editor (Alt+F11) and add following handler in Normal -> Microsoft Word Objects -> ThisDocument:

    Private Sub Document_Close()
       MsgBox "Close " & Me.Name
    End Sub

    3. Close editor and switch back to empty document.

    4. Create another empty document by Ctrl+N and close it. The message box should appear.

    5. Create another empty document by clicking pinned taskbar icon (see picture below). Close it. No message box appear.

    Task bar Icon

    If you have any ideas how this issue can be worked around - I would be highly appreciate.

    Wednesday, June 28, 2017 11:16 AM

All replies

  • Hi Radosvet,
    I can reproduce your issue. I suggest you create a new document via VBA code Documents.Add or you could select File->New to create a new document.
    Best Regards,
    Terry
    Thursday, June 29, 2017 10:38 AM
  • Thank you Terry.

    But if program is supposed to be run by other users on different environment, this scenario cannot be guaranteed. So, I need more robust solution to track Document.Close event.

    Thursday, June 29, 2017 11:30 AM
  • Hi Radosvet,

    I think you could use VSTO add-ins as a workaround. A Word VSTO add-ins could extend features of the word application. We could add document close event to word application, so each file created by this application will have the document close event. You could see an example from here Walkthrough: Creating Your First VSTO Add-in for Word. Here is my code for adding Document.Close event.

    private void ThisAddIn_Startup(object sender,System.EventArgse)
    {        
    //get application object         
    Word.Application wdApp = Globals.ThisAddIn.Application;          
    //add document close event          
    wdApp.DocumentBeforeClose += WdApp_DocumentBeforeClose;
    }
    private void WdApp_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
    {         
    MessageBox.Show("Close:"+Doc.Name );
    }

    Best Regards,

    Terry

    Monday, July 3, 2017 6:53 AM
  • Hi Radosvet,

    you had mentioned that,"It looks like I found a problem with Document.Close event."

    I try to make a test with different version of MS Word.

    with all the versions I got the same output.

    I find that Normal Project work as a Template.

    so whenever you try to press Ctrl + N or try to create document from 'New' option it generates the new document based on that Normal project.

    so when you close the document you get a message on close.

    but when you try to create document from the .EXE file at that time it creates a document which is independent from that Normal Project.

    so when you close it Document_Close event will not fire.

    to taste the same.

    you can put code below in normal project.

    Private Sub Document_New()
    MsgBox ("new document created")
    End Sub
    

    you will find that when you press Ctrl + N or click 'New' to create document , you will get message that new document created.

    because document generated based on normal template.

    but when you try to create a new document using .EXE file you will not get this message because that newly created document is not depend on that Normal Project.

    I hope this will clear your doubt regarding this behavior of Word application.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 5, 2017 6:52 AM
    Moderator
  • Hi Terry,

    Thank you for your reply, and excuse for my delay. I don't VSTO would be a valid workaround:

    1. Word engine raises events the same way for all types of software interfaces, VSTO or VBA;

    2. VSTO redistribution and deployment is a source of many other issues. So a set of Macros can be a much better solution most cases;

    But still, thank you for your time and help.

    Kind regards,

    Radosvet


    • Edited by Radosvet Monday, July 10, 2017 7:50 PM
    Monday, July 10, 2017 7:43 PM
  • Hi Deepak,

    Thank you for your reply and please excuse my delay. You are right that the issue cannot be reproduced this way. But if you close all Word instances with you macro left in Normal.dotm and start new Word instance and create a new document - your macro will be called. BUt for all other instances created from new process it won't work, as you mentioned earlier.

    As far as only one Word process runs at a time, my guess is the problem lies somewhere in the initialization code of the Word, triggered by "pipe" from launching process.

    So, it definetly appears to be a bug.

    Kind regards,

    Radosvet

    Monday, July 10, 2017 7:49 PM
  • Hi Radosvet,

    you had mentioned that," if you close all Word instances with you macro left in Normal.dotm and start new Word instance and create a new document - your macro will be called."

    I can reproduce this behavior on my side with Word 2010, 2013, 2016.

    but what I think here is that , it is some kind of limitation instead of bug or some kind of security related feature.

    that may be not documented for users.

    because we can get the same result with all the Office versions.

    if it is a bug then it is possible that it can affect some of versions and not all.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 11, 2017 6:15 AM
    Moderator
  • Hi Deepak,

    Thank you for your reply. I see your point, but I cannot fully agree on that. There are lots of other limitations, which are outdated and cause failures on many scenarios. For instance, 256 chars max file path length and all Office versions. Word cannot handle paths longer than this. But together with SharePoint such limitation cause failure to open MUCH shorter file paths if they contain Unicode symbols.

    And in my case - if I need a macro which must be run when any document is closed, such limitation causes failures. From this point of view - it should be marked as a bug, or should mentioned as a limitation in the Word documentation.

    Regards,

    Radosvet

    Tuesday, July 11, 2017 7:44 AM