Tarun Sharma

Tarun Sharma

  • NA
  • 13
  • 2.3k

document.close() and application.quite() event in word addin

Sep 20 2018 1:06 AM
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.
 
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;
}
}
}
 

Answers (1)