Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am not able to identify which close event is fired document.close() or application close(X) button.

1. Close only word document if user close it by using File-->Close option not complete
application.
2. When user opened a document and write some text in it and click on application
Close(X) button, then application should prompt the message to ask user to "save
this document "YES" or "NO"? if user click on "NO" button then complete application
must be close.

Note: I have also implement the "
ApplicationEvents4_QuitEventHandler
application quite method but it also doesn't work for me.

What I have tried:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
			((ApplicationEvents4_Event)this.Application).NewDocument += new ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
            this.Application.DocumentOpen += new ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
            this.Application.DocumentBeforeClose += new ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);               
            this.Application.WindowActivate += new ApplicationEvents4_WindowActivateEventHandler(Application_WindowActivate);
            this.Application.WindowDeactivate += new ApplicationEvents4_WindowDeactivateEventHandler(Application_WindowDeactivate);
            this.Application.DocumentBeforeSave += new ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);
            //((ApplicationEvents4_Event)this.Application).Quit += new ApplicationEvents4_QuitEventHandler(Application_Quit);
		}
		
		void Application_DocumentBeforeClose(Document document, ref bool cancel)
        {
			if (document.Content.Characters.Count <= 1)
            {
                document.Close(WdSaveOptions.wdDoNotSaveChanges);
            }
            else
            {
                DialogResult DialogMessage = MessageBox.Show("Do you want to save document?", "Testing",
                                             MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                if (DialogMessage == DialogResult.Yes)
                {
                    bool SaveAsUI = true;
                    bool Cancel = false;
                    cancel = true;
                    Application_DocumentBeforeSave(document, ref SaveAsUI, ref Cancel);
                }
                else if (DialogMessage == DialogResult.No)
                {
                    cancel = true;
                    if (this.Application.Documents.Count == 1)
                    {
                        document.Close(WdSaveOptions.wdDoNotSaveChanges);                                
                        //this.Application.Quit();
                    }
                    else
                    {
                        document.Close(WdSaveOptions.wdDoNotSaveChanges);
                    }
                }
                else
                {
                    cancel = true;
                }
            }
		}
Posted
Comments
Maciej Los 20-Sep-18 2:52am    
it also doesn't work for me. is not decriptive at all! Please, be more specific and provide more details.
Note: MS Word is multi-instancing application. So, when you open or create new document, it's creating new intance of MS Word (by default). See: A new instance of Word appears to run when you create or open an additional document in Word 2000 and in later versions of Word[^]
TSharma0407 15-Oct-18 0:09am    
I just want to track which event is fired, file close or application quite/close(X) event before document close.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900