none
Save Excel file without asking to overwrite it

    Question

  • Hey,

     

    I'm manipulating an Excel (.xls) file trough C#, and I'm using this function the save the file in the end of my program:

     

    excelWS.SaveAs(@path, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

    But after it the windows prompt asking to the user if he would like to overwrite the existing file (because i'm saving it with the same name as before).

     

    I'd like to know if there's a way to always overwrite the file, without asking to the user.

    Regards

    Thursday, April 29, 2010 1:55 PM

Answers

  •    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    
       excel.DisplayAlerts = false;
    
       excelSheePrint.SaveAs(filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
    
    
    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same; My Code Blog:http://nauhil.wordpress.com/

    • Proposed as answer by Huan Li Friday, April 30, 2010 1:23 PM
    • Marked as answer by Henrique Nunes Friday, April 30, 2010 2:44 PM
    Friday, April 30, 2010 1:06 PM
  • As a work around

    you use File.Exist() to check whether is there any file avaible with the same name, then just delete it File.Delete


    that is the only way left. i'm adding sample code. {found here http://www.daniweb.com/forums/thread208167.html }
    Friday, April 30, 2010 1:20 PM

All replies

  • As a work around

    you use File.Exist() to check whether is there any file avaible with the same name, then just delete it File.Delete

    • Proposed as answer by AdityaReddyM Friday, April 30, 2010 7:30 AM
    Friday, April 30, 2010 7:30 AM
  •    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    
       excel.DisplayAlerts = false;
    
       excelSheePrint.SaveAs(filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
    
    
    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same; My Code Blog:http://nauhil.wordpress.com/

    • Proposed as answer by Huan Li Friday, April 30, 2010 1:23 PM
    • Marked as answer by Henrique Nunes Friday, April 30, 2010 2:44 PM
    Friday, April 30, 2010 1:06 PM
  • As a work around

    you use File.Exist() to check whether is there any file avaible with the same name, then just delete it File.Delete


    that is the only way left. i'm adding sample code. {found here http://www.daniweb.com/forums/thread208167.html }
    Friday, April 30, 2010 1:20 PM
  • Thank you all!

    It worked =)
    Friday, April 30, 2010 2:44 PM
  •   Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    
      excel.DisplayAlerts = false;
    
      excelSheePrint.SaveAs(filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
    
    
    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same; My Code Blog:http://nauhil.wordpress.com/
    

    Hi, I'm trying to open a Excel file, make changes, then save this file.

    After playing with the arguments for SaveAs(), so the file name is the same as the one opened,

    i cannot find a way to really save my changes.

    What're the correct arguments pls ?

    Saturday, June 18, 2011 5:51 PM
  • You also need to add "excel.DisplayAlerts = true;" after the SaveAs.  If you don't, then you can disable prompts which you may need to see.

    Monday, September 30, 2013 4:10 PM