none
Workbook.SaveAs() suppress overwrite file dialog RRS feed

  • Question

  • I am using C# Visual Studio 2013 and Visual Studio Tools for Office to create a new workbook in code.

    How can i suppress the "file already exists... do you want to overwrite" message? when i save a workbook?

    Workbook.ConflictResolution = Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges;

    Globals.ThisAddIn.Application.DisplayAlerts = false;

    Workbook.SaveAs(sFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing);





    • Edited by ResQue1980 Wednesday, May 7, 2014 4:24 PM
    Wednesday, May 7, 2014 4:21 PM

Answers

  • Hi,

    According to your description, you want to suppress the "file already exists... do you want to overwrite" message when saving a workbook.

    After testing, I find if you set the DisplayAlerts as false, it won't pop up the "file already exists... do you want to overwrite" message, and it will default to save the workbook to overwrite the existing one. Do you mean that setting the DisplayAlerts doesn't work for you? If so, I suggest you checking your code or sharing the whole code with us to troubleshoot.

    private void TestSaveAs()
    {
        Excel.Workbook Workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
        Workbook.ConflictResolution = Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges;
    
        Globals.ThisAddIn.Application.DisplayAlerts = false;
    
        string sFileName = @"C:\Users\test\Documents\pop.xlsx";
        Workbook.SaveAs(sFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 9, 2014 6:04 AM
    Moderator

All replies

  • Hello,

    You can check whether such file exists on the disk before calling the SaveAs method. If it exists, you can just delete it programmatically (see System.IO classes) and then call the SaveAs method.

    • Proposed as answer by SmallCloud Thursday, June 20, 2019 7:18 AM
    Wednesday, May 7, 2014 8:45 PM
  • Hi,

    According to your description, you want to suppress the "file already exists... do you want to overwrite" message when saving a workbook.

    After testing, I find if you set the DisplayAlerts as false, it won't pop up the "file already exists... do you want to overwrite" message, and it will default to save the workbook to overwrite the existing one. Do you mean that setting the DisplayAlerts doesn't work for you? If so, I suggest you checking your code or sharing the whole code with us to troubleshoot.

    private void TestSaveAs()
    {
        Excel.Workbook Workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
        Workbook.ConflictResolution = Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges;
    
        Globals.ThisAddIn.Application.DisplayAlerts = false;
    
        string sFileName = @"C:\Users\test\Documents\pop.xlsx";
        Workbook.SaveAs(sFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 9, 2014 6:04 AM
    Moderator