Start New Discussion within our Software Development Community

Hello all,

I am using Office.Interop.Excel.Application in which i have to save a file using saveas() but before saving it i have to disable the Excel's OverWrite prompt . I did tries Application.DisplayAlerts = false;

But it's not working , How to do this???
Pl Help ?

Try this:

private void DoExcel()
      const string Fname = @"C:\dotnetwin\daniweb\Book1.xlsx";
      Microsoft.Office.Interop.Excel.ApplicationClass excel = null;
      Microsoft.Office.Interop.Excel.Workbook wb = null;
      Microsoft.Office.Interop.Excel.Worksheet ws = null;
      Microsoft.Office.Interop.Excel.Range rng = null;

      object missing = Type.Missing;
      //bool ReadOnly = false; //true or missing gives the same
        //Excel.Application is abstract class so I use this
        excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
        //If I use Open or _Open it gives the same
        wb = excel.Workbooks.Open(Fname,
          missing, //updatelinks
          false, //readonly
          missing, //format
          missing, //Password
          missing, //writeResPass
          true, //ignoreReadOnly
          missing, //origin
          missing, //delimiter
          true, //editable
          missing, //Notify
          missing, //converter
          missing, //AddToMru
          missing, //Local
          missing); //corruptLoad
        ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
        rng = ws.get_Range("A1", missing);
        rng.Value2 = "Some string";
        //All is well until here, Save thinks the excelfile is readonly
        string tmpName = Path.GetTempFileName();
        wb.SaveAs(tmpName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
        wb.Close(false, missing, missing);
        File.Move(tmpName, Fname);
      catch (Exception ex)
        MessageBox.Show("Error: " + ex.ToString());

The same question was asked here:

Hello ,

I am not opening any new workbook, just working on current workbook ....

It doesn't matter if you open an existing workbook or create a new one. Either way you still need to call .SaveAs() which is going to give you the overwrite prompt unless you do something like the code I posted above.


You said that you used "Application.DisplayAlerts = false;"
That wont work, you need to use the name of your newly declared excel application

excel.DisplayAlerts = false;

That should work nicely for you!

It's conceptually simpler to use these two lines immediately before the SaveAs.

var fi = new FileInfo(fileName);
if(fi.Exists) File.Delete(fileName);

try this code

            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[1, 1] = "Sheet 1 content";

            xlWorkBook.SaveAs("yourFileName", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);

Full Source......Create Excel Worksheet