Attempting to exit C # Excel Workbook without a dialog box

I use Microsoft Office Interop to edit some excel files, and when I close them I use

outputExcelWorkBook.Close(false, paramMissing, paramMissing); 

But a dialog box still appears, although I passed false as the first parameter. I also tried it with true and gave it the path to the file as the second parameter, but in both cases a dialog box appears asking if I want to save until closing. Can someone tell me what is going on here?

+8
c # excel-interop excel-automation
source share
2 answers

Try setting the Application.DisplayAlerts property to false . You may find it useful to set this property to false for most of your automation procedures. 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; } 
+12
source share

This worked for me:

  • Initiate Excel

  • Open book

  • Get the active sheet and do the editing ("Text" has been added to the cell [2,2])

  • Close the book with one true parameter, which means "save changes"

  • A dialog box does not appear.

Note. When I call Close without a parameter, I am prompted to save the 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(); 
+3
source share

All Articles