none
How to close Excel Application? RRS feed

  • Question

  • HI ALL

    I am working with Excel VSTO when I close a workbook document

    Application_WorkbookBeforeClose event will be triggered and it will show

    custom popup with saving, don't save and cancel as button options.

    If I click on don't save document will be closed without saving the changes to the document

    This is the code I am using for ignoring and Close excel application

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.WorkbookBeforeClose += Application_WorkbookBeforeClose;
    }
    
    private void Application_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
    {
        DialogResult result = MessageBox.Show("Do you want to save changes to " + Wb.Name + "?", "Microsoft Excel ", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
    
        switch (result)
        {
            case DialogResult.Yes:
    
                Wb.Save();
                break;
            case DialogResult.No:
                int count = this.Application.Workbooks.Count;
                if(count > 0)
                {
    
                    if (count == 1)
                    {
                        Excel.Application excel = this.Application;
                        Wb.Close(false, missing, missing);
                        excel.Quit();
                        excel = null;
                    }
                    else
                    {
                        Wb.Close(false, missing, missing);
                    }
                }
                break;
    
            case DialogResult.Cancel:
                Cancel = true;
                break;
        }
    }






    If more than one excel document is opened for edit, then Excel vsto addin should close the particular excel document in which the close actions are performed and other documents should remain open.
    How to close the Excel Application completely if only one document is opened?




    Tuesday, February 12, 2019 6:41 AM