30
votes

I have an Excel workbook with a custom non-time-dependent cell function that I am opening from a C# WindowsForms application using Interop.Excel. I read four values from it, perform no explicit changes/calculations, then close it from C#.

When I try to directly close (without saving), I get a save prompt. I suspect this is because Excel is interpreting auto-recalculation on open as creating a change, even though nothing actually numerically or formulaically changes. I also set Excel.Application.Calculation = Excel.XlCalculation.xlCalculationManual. How do I prevent the save prompt from appearing and just close-without-save?

1
you can also do a workbook.Saved = true before saving to let excel app know that this is already saved and no need to prompt..nawfal

1 Answers

59
votes

When you use the Excel objects, be sure to close the workbook like this:

Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add("Yourworkbook");

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

....do your stuff

xlWorkBook.Close(false, misValue, misValue);

xlApp.Quit();

The false in the close method indicates don't save changes.