Trying to exit C# Excel Workbook without a dialog box
Try setting the Application.DisplayAlerts
property to false
. You may find it useful to set this property to false
for the majority of your automation routines. Remember to restore the previous value before returning.
Application applicationInstance = ...;
var oldDisplayAlertsValue = applicationInstance.DisplayAlerts;
applicationInstance.DisplayAlerts = false;
try
{
outputExcelWorkBook.Close(false, Missing.Value, Missing.Value);
}
finally
{
appliationInstance.DisplayAlerts = oldDisplayAlertsValue;
}
This worked for me:
Initiate Excel
Open the workbook
Get the active sheet and make an edit (added "Text" to cell [2,2])
Close the workbook with a single parameter of true which means "save changes"
No dialog box is displayed.
Note: When I call Close without a parameter I am prompted to save changes.
Microsoft.Office.Interop.Excel.Application excel = new Application();
Microsoft.Office.Interop.Excel.Workbook workBook =
excel.Workbooks.Open(fileLocation);
Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.ActiveSheet;
sheet.Cells[2, 2] = "Text";
workBook.Close(true);
excel.Quit();