none
'Document1' automatically closed if user opens a saved document. Close Event does not fire RRS feed

  • Question

  • Hi,

    Close Events never gets fired Steps:

    Open New word ( press Window+R type winword and press ENTER).

    Don't edit the document, it would show title 'Document1'.

    Open any word document from desktop, 'Document1' will be closed, and the document from desktop will be opened.

    I need to know when 'Document1' was closed, so that i can perform some action. The available Event 'DocumentBeforeClose' never gets fired in this case. Help me how to resolve this issue.


    -- Alok Tripathi

    Thursday, September 24, 2015 1:47 PM

Answers

  • Hi Alok

    <<but the workaround now create another problem...If you close the document, it will now ask you to save. technically we didn't edit then why i should restrict user to click on SAVE/Don't Save.>>

    Yes, technically the document HAS been edited - and in my reply I mentioned that a prompt will appear.

    <<So if this is not a bug and is a feature, then why it is not true for 'Document 2'.>>

    It sounds trite, I realize, but: because that's the way Microsoft programmed Word to work. Document 1 is "temporary", generated by default when Word starts. If the user does anything to change its "saved" state, then it's not closed. If the user directly opens a different document, it's closed. But Document 2 (and later) are generated specifically by the user, so they're not closed automatically.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Alok Tripathi Friday, October 9, 2015 4:37 AM
    Thursday, October 8, 2015 5:41 PM
    Moderator
  • Hi Alok Tripathi,

    Thanks for the detail information for this issue. I am able to reproduce this issue.

    As a workaround, we can add some text and delete them in the document add event. Here is the code for your reference:

      void ThisAddIn_NewDocument(Word.Document Doc)
            {
                Doc.Content.InsertAfter(" ");
                Doc.Content.Delete();
            }

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 29, 2015 6:34 AM
    Moderator
  • This is expected behavior on the part of Word - just so we're clear that we're not dealing with a bug...

    An even more elegant approach than writing and deleting might be to set Document1.Saved = false.

    But in both cases, it's important to realize that a prompt to save the document will be triggered.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 5, 2015 5:56 PM
    Moderator

All replies

  • Hi Alok Tripathi,

    The event works well for me. Would you mind sharing how you coding for this event? Here is two ways that worked for me:
    1. Copy the code below to ThisDocument module of Document1

    Private Sub Document_Close()
    MsgBox ""
    End Sub
    2. Create a class module(Class1) as below
    Public WithEvents appWord As Word.Application
     
    Private Sub appWord_DocumentBeforeClose _
            (ByVal Doc As Document, _
            Cancel As Boolean)
     
       MsgBox "Hello"
    End Sub
    
    
    Then create a module to initialize the event like code below:
    Dim a As Class1
    Sub test()
    Set a = New Class1
    Set a.appWord = Application
    
    End Sub
    
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 25, 2015 1:57 AM
    Moderator
  • Hi, 

    Thanks for reply.

    The Close event gets fire properly when user clicks on Close icon( X icon). My problem is that this event never gets fired when Micorsoft 'automatically closes a document'. When i say 'Automatically closes a document' it need to perform following steps(As mentioned in original post).

    Open New word ( press Window+R type winword and press ENTER).
    Don't edit the document, it would show title 'Document1'.
    Open any word document from desktop, 'Document1' will be closed, and the document from desktop will be opened.

    Here in last line, the first document gets closed automatically but in this scenario Close Event does not fired. I need to perform some action when 'Document1' gets closed automatically.

    Hope this elaborate my problem precisely. I will try to write a sample code if this is not helping to understand. 

    Thanks in advance.


    -- Alok Tripathi

    Friday, September 25, 2015 7:22 AM
  • Hi Fei Xue,

    Please find sample code below.

    TO use this code what i did, created a Word Add-in project, and added following code lines inside class ThisAddIn.cs

                                                                       

    Please copy entire code and replace with class content

    -------------------------------
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;


    namespace WordAddIn1
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                ((Word.ApplicationEvents4_Event)this.Application).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(ThisAddIn_NewDocument);
                this.Application.DocumentOpen +=new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
                this.Application.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);
                this.Application.WindowActivate += new Word.ApplicationEvents4_WindowActivateEventHandler(Application_WindowActivate);
                this.Application.WindowDeactivate += new Word.ApplicationEvents4_WindowDeactivateEventHandler(Application_WindowDeactivate);
                this.Application.DocumentBeforeSave += new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
                ((Word.ApplicationEvents4_Event)this.Application).Quit += ThisAddIn_Quit;
            }

            void ThisAddIn_Quit()
            {

            }

            void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {

            }

            void Application_WindowDeactivate(Word.Document Doc, Word.Window Wn)
            {

            }

            void Application_WindowActivate(Word.Document Doc, Word.Window Wn)
            {

            }

            void Application_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
            {
                //This method will never call, if we follow steps as in earlier post, and 'Document1' will be closed I need to show this alert whenever a document gets closed.
                MessageBox.Show("Bye Bye " + Doc.FullName);
            }

            void Application_DocumentOpen(Word.Document Doc)
            {

            }

            void ThisAddIn_NewDocument(Word.Document Doc)
            {

            }

            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                ((Word.ApplicationEvents4_Event)this.Application).NewDocument -= new Word.ApplicationEvents4_NewDocumentEventHandler(ThisAddIn_NewDocument);
                this.Application.DocumentOpen -= new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
                this.Application.DocumentBeforeClose -= new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);
                this.Application.WindowDeactivate -= new Word.ApplicationEvents4_WindowDeactivateEventHandler(Application_WindowDeactivate);
                this.Application.DocumentBeforeSave -= new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
            }



            #region VSTO generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }

            #endregion
        }
    }

    ----------


    -- Alok Tripathi

    Monday, September 28, 2015 4:55 AM
  • Hi Alok Tripathi,

    Thanks for the detail information for this issue. I am able to reproduce this issue.

    As a workaround, we can add some text and delete them in the document add event. Here is the code for your reference:

      void ThisAddIn_NewDocument(Word.Document Doc)
            {
                Doc.Content.InsertAfter(" ");
                Doc.Content.Delete();
            }

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 29, 2015 6:34 AM
    Moderator
  • This is expected behavior on the part of Word - just so we're clear that we're not dealing with a bug...

    An even more elegant approach than writing and deleting might be to set Document1.Saved = false.

    But in both cases, it's important to realize that a prompt to save the document will be triggered.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 5, 2015 5:56 PM
    Moderator
  • Thanks Cindy/Fei Xui,

    I apologize for delayed reply... but the workaround now create another problem...If you close the document, it will now ask you to save. technically we didn't edit then why i should restrict user to click on SAVE/Don't Save.

    Second, repest all the steps above but for Document 2( not for Document 1), it will never occur, what i want to say, Document 2 never gets closed.

    So this scnerio is only replicating when there is 'Document 1' and this will never happen when there is 'Document 2' or 3,4 and so on.

    So if this is not a bug and is a feature, then why it is not true for 'Document 2'.

    Thanks in advance for any further answer/suggestion


    -- Alok Tripathi

    Thursday, October 8, 2015 4:30 PM
  • Thanks Cindy/Fei Xui,

    I apologize for delayed reply... but the workaround now create another problem...If you close the document, it will now ask you to save. technically we didn't edit then why i should restrict user to click on SAVE/Don't Save.

    Second, repest all the steps above but for Document 2( not for Document 1), it will never occur, what i want to say, Document 2 never gets closed.

    So this scnerio is only replicating when there is 'Document 1' and this will never happen when there is 'Document 2' or 3,4 and so on.

    So if this is not a bug and is a feature, then why it is not true for 'Document 2'.

    Thanks in advance for any further answer/suggestion


    -- Alok Tripathi

    Thursday, October 8, 2015 4:31 PM
  • Hi Alok

    <<but the workaround now create another problem...If you close the document, it will now ask you to save. technically we didn't edit then why i should restrict user to click on SAVE/Don't Save.>>

    Yes, technically the document HAS been edited - and in my reply I mentioned that a prompt will appear.

    <<So if this is not a bug and is a feature, then why it is not true for 'Document 2'.>>

    It sounds trite, I realize, but: because that's the way Microsoft programmed Word to work. Document 1 is "temporary", generated by default when Word starts. If the user does anything to change its "saved" state, then it's not closed. If the user directly opens a different document, it's closed. But Document 2 (and later) are generated specifically by the user, so they're not closed automatically.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Alok Tripathi Friday, October 9, 2015 4:37 AM
    Thursday, October 8, 2015 5:41 PM
    Moderator
  • Thanks Cindy,

    As this is by design and MS word feature, we will need to live with this scenario. I am marking this as answer.



    -- Alok Tripathi

    Friday, October 9, 2015 4:37 AM