Jack Huang Jack Huang - 1 month ago 8
C# Question

How to close-without-save an Excel /xlsm workbook, w/ a custom function, from C#

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?

Answer Source

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);


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