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 ?

Recommended Answers

All 6 Replies

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
      try
      {
        //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();
        File.Delete(tmpName);        
        wb.SaveAs(tmpName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
        wb.Close(false, missing, missing);
        excel.Quit();
        File.Delete(Fname);
        File.Move(tmpName, Fname);
      }
      catch (Exception ex)
      {
        MessageBox.Show("Error: " + ex.ToString());
      }
    }

The same question was asked here:
http://www.daniweb.com/forums/thread196171.html

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.

Hi,

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;
                wb.SaveAs("C:\\Test.xls",Excel.XlFileFormat.xlWorkbookNormal,null,null,false,false,Excel.XlSaveAsAccessMode.xlShared,false,false,null,null,null);

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

Lee

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.