none
Missing dialog when saving two workbooks with the same name (VSTO 4.0, .Net 3.5, Excel 2007 and Excel 2010) RRS feed

  • Question

  • Hi,

    following Code in a new Excel Add-In:

     private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
            }
    
            void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
            {
                Application.WorkbookBeforeSave -= new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
                var dialogBox = Globals.ThisAddIn.Application.Dialogs[XlBuiltInDialog.xlDialogSaveWorkbook];
                dialogBox.Show();
                Cancel = true;
                Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
                
            }

    Steps to reproduce:

    • Start Excel with the add-in
    • Save Workbook in a folder (e.g. c:\foo\) with name "excel.xlsx"
    • Create a second workbook with File -> New -> Workbook
    • Save new workbook in another folder but with the same name (e.g. c:\foo\bar\excel.xlsx)

    Usually there comes a dialog which says that you cannot save the second workbook with the same name as the first. If you use Dialog.Show() for saving, you can click the save button but nothings happens. No error dialog pops up.

    Can someone reproduce this issue and is there any workaround for this?

    Thanks in advice and

    best regards

    Mark Altmann


    Viele Grüße Mark



    • Edited by Wompi Wednesday, July 25, 2012 12:14 PM
    Wednesday, July 25, 2012 12:13 PM

All replies

  • Hi Viele,

    Thanks for posting in the MSDN Forum.

    I can reproduce your issue on my side. I will consult it in internal channel to see whether there have work round. There might be some time delay, appreciate for your patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 27, 2012 6:37 AM
    Moderator
  • Hi Mark,

    Please review your steps to duplicate.  The original Excel workbook is saved in ...\foo.

    The second workbook with the same name is saved in ...\foo\bar

    On a machine without your add-in, repeat the steps through the Excel User interface.

    The workbooks will be saved without any pushback from Excel.  This is a function of MSOffice, which is the Office interface to the file system of Windows. The operating system provides for saving a file in numerous folders.

    Try the same exercise with the destination folder both times being ..\foo.  Then you'll get the "confirm Save As' dialog.

    By the way, please explain the purpose of the declaration of the delegate for the

    Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);

    event handler within the existing event handler - see the last line of your  event handler delegate.  Thanks.

    Regards,


    Please remember to mark the replies as answer if they help and unmark them if they provide no help. and click "Vote as Helpful" this and other helpful posts, so other users will see your thread as useful. Best Regards, Chris Jensen


    Friday, July 27, 2012 5:34 PM
    Moderator
  • Hi,

    @Tom: No problem, thank you very much.

    @cjatms:
    With Excel User interface do you mean open the usual program excel or do you mean another thing? Unfortunately in excel I cannot open two files with the same name.

    For testing purpose I remove/add the delegate. In my productive code I use the  Application.EnableEvents field to suppress the events. Otherwise the Dialog.Save() function would throw another Save event which will be catched by my BeforeSave-Eventhandler.

    Best regards

    Mark Altmann


    Viele Grüße Mark


    • Edited by Wompi Tuesday, July 31, 2012 12:42 PM
    Tuesday, July 31, 2012 12:42 PM
  • Hi Mark,

    Yes - please repeat the steps in my reply, above, with an instance started through the user interface - or, in a different way of expressing the steps - open Excel manually.  Do this on a system without your add-in, or if you don't have one without the add-in, disable loading the add-in thus: Start Excel, Click the 'File' button, in the backstage click 'Options', in the 'Options' list click 'Add-ins',
    at the top of the "Add-Ins list box you'll see your add-in in the list of 'Active Application Add-ins'. At the bottom of the Add-ins list box there is an area labelled 'Manage' with a drop-down set to either 'Excel add-ins' or 'COM add-ins'. Select 'COM add-ins' and click the 'Go...' button.  In the COM Add-in dialog, in the Add-ins available list click to uncheck your add-in. Click 'OK' and exit Excel.

    Then start the exercise described in my earlier reply - i.e. start a new instance of Excel.

    Save the empty workbook with any name in a parent folder.

    Close Excel.

    Start Excel again and save the empty workbook with the same name as you used before in a child folder of the parent folder.

    You will not see the 'Confirm Save as' dalog since you haven't tried to save a workbook with a name identical to the one already in the parent folder. You can save a file with a specific name into as many folders as required so long as there isn't one with the same name already in the folder.

    You are correct when you say that in Excel you cannot open two versions of a file with the same name. That is not part of the exercise in this reply. Your original post discusses saving a workbook, not opening a workbook. Do you wish to change the issue in this thread?


    Please remember to mark the replies as answer if they help and unmark them if they provide no help. and click "Vote as Helpful" this and other helpful posts, so other users will see your thread as useful. Best Regards, Chris Jensen

    Tuesday, July 31, 2012 3:24 PM
    Moderator
  • Hi Chris,

    Thank you very much. Now I understand what you mean.

    Yes with your exercise I can save two files into different folders, and also the "Save as" dialog pops up if I try to save the workbook into the same folder.

    But I still don't understand how this should be the solution for my question.

    I have asked, if you want to save two workbooks (within the same excel instance) with the same name in excel without overwriting the save dialog with Dialog.Show() there comes a dialog "You cannot save the workbook with the same name as another open workbook". If I overwrite the Dialog.Show() then the above dialog doesn't pop up and nothing happens. I think this would be an issue for a customer with this addin if he doesn't get informed what is wrong and just cannot save the workbook. So I am looking for a workaround.

    To reproduce this issue please see my first post.

    Thank you and

    Best regards

    Mark Altmann


    Viele Grüße Mark



    • Edited by Wompi Wednesday, August 1, 2012 7:20 AM
    Wednesday, August 1, 2012 7:18 AM
  • Hi Mark,

    Your add-in works with Excel. Excel runs on Windows. Windows implements file management. Why do you need to overwrite system messages? Suggest you don't overwrite the Operating System's messages.


    Please remember to mark the replies as answer if they help and unmark them if they provide no help. and click "Vote as Helpful" this and other helpful posts, so other users will see your thread as useful. Best Regards, Chris Jensen

    Wednesday, August 1, 2012 2:31 PM
    Moderator
  • Hi Chris,

    unfortunately if the user saves the workbook we have to save additional information for our addin.

    If the user cancels the save dialog we have to clean up this information.

    As long as there isn't any AfterSave event I think this is the only possibility to handle this.

    Regards,

    Mark


    Viele Grüße Mark

    Wednesday, August 1, 2012 2:42 PM
  • Hello Mark,

    No workaround suggests itself. Perhaps other viewers of this thread can suggest alternatives.


    Please remember to mark the replies as answer if they help and unmark them if they provide no help. and click "Vote as Helpful" this and other helpful posts, so other users will see your thread as useful. Best Regards, Chris Jensen

    Friday, August 3, 2012 3:47 PM
    Moderator