1.11M Members

how to Disable Excel Overwrite Promt

 
0
 

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 ?

 
0
 

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

 
0
 

Hello ,

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

 
0
 

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.

 
0
 

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!

 
0
 

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

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

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article