Reputation: 83
The following piece of code to save an excel file still throws a pop up box asking if I should overwrite the existing file. How can I auto overwrite the existing file without the popup being thrown?
xlWorkBook.SaveAs(@"C:\Temp\csharp-Excel.xls"
, XlFileFormat.xlWorkbookNormal
, misValue
, misValue
, misValue
, misValue
, XlSaveAsAccessMode.xlExclusive
, XlSaveConflictResolution.xlLocalSessionChanges
, misValue
, misValue
, misValue
, misValue);
Upvotes: 0
Views: 4810
Reputation: 32713
You can turn off the DisplayAlerts
property on the application object. For example:
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.DisplayAlerts = false;
As @Degustaf has pointed out: It is important to run excel.DisplayAlerts = True
after your SaveAs
code runs.
Also, you can use System.IO.File.Exist()
to check if that file exists and then just delete it with File.Delete
.
For example:
var fileName = @"C:\Temp\csharp-Excel.xls";
if (File.Exists(fileName)) File.Delete(fileName);
This will ensure that there is no file of the same name, so you can overwrite it without a prompt.
Upvotes: 2