none
Blokcing the Save, Don't Save, Cancel dialog RRS feed

  • Question

  • I am running with the latest versions of VSTO, Office and Visual Studio.

    All the data for my workbook comes from and is saved to a database. When data is loaded Excel, rightly, detects that changes have been made and when the user closes the spreadsheet, the Save, Don't Save, Cancel dialog comes up. I want to block this dialog box because I don't want the user to save the spreadsheet. I have found the Cancel = true on the WorkbookBeforeSave event prevents the save from occurring, but I want to block the dialog entirely. Can I do that?

    Friday, March 15, 2013 5:02 PM

Answers

  • Hi Jeff

    Right, sorry, my brain called up the wrong picture...

    Set the Workbook.Saved property = True. This tells Excel there are no changes to be saved, which should prevent the prompt from appearing.

    As far as I know, there's no way to suppress this prompt if there are things Excel thinks need to be saved. So if the user would make a change in the workbook after your code has set this property, the prompt would still appear...


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, March 17, 2013 6:37 AM
    Moderator
  • That works! I am able to prevent the user for ever saving the worksheet. My code is:

           // On close indicate to Excel that the workbook has been saved so we don't get a prompt to save.
            void Application_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
            {
                Globals.ThisWorkbook.Saved = true;
            }

            // On save block the UI and cancel the save.
            void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
            {
                SaveAsUI = false;
                Cancel = true;
            }

    • Marked as answer by jeffki Monday, March 18, 2013 3:54 PM
    Monday, March 18, 2013 3:53 PM

All replies

  • THat's what the SaveAsUI parameter is for. Set that to False and it should suppress the dialog box...

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, March 15, 2013 6:03 PM
    Moderator
  • I tried that and it does not suppress the dialog. I think it suppress the save as dialog. I want something in the WorkbookBeforeClose event that will stop the save, don't save, cancel dialog.
    Friday, March 15, 2013 7:40 PM
  • Hi Jeff

    Right, sorry, my brain called up the wrong picture...

    Set the Workbook.Saved property = True. This tells Excel there are no changes to be saved, which should prevent the prompt from appearing.

    As far as I know, there's no way to suppress this prompt if there are things Excel thinks need to be saved. So if the user would make a change in the workbook after your code has set this property, the prompt would still appear...


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, March 17, 2013 6:37 AM
    Moderator
  • That works! I am able to prevent the user for ever saving the worksheet. My code is:

           // On close indicate to Excel that the workbook has been saved so we don't get a prompt to save.
            void Application_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
            {
                Globals.ThisWorkbook.Saved = true;
            }

            // On save block the UI and cancel the save.
            void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
            {
                SaveAsUI = false;
                Cancel = true;
            }

    • Marked as answer by jeffki Monday, March 18, 2013 3:54 PM
    Monday, March 18, 2013 3:53 PM