Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 408
Jack Huang Avatar asked Jan 11 '12 03:01

Jack Huang


People also ask

How do you close Excel without saving?

Press close button while holding Shift key, then press Don't save (still holding Shift ).

How do you close an Excel File without saving using VBA?

You can also use this for closing workbook without saving changes. ~Sub CloseBook2() ActiveWorkbook. Close savechanges:=False End Sub~ This routine can be attached to Close X Button.

How do I turn off Save Changes prompt when I close a workbook in Excel?

In Microsoft Excel, you can create a Microsoft Visual Basic for Applications (VBA) macro that suppresses the Save Changes prompt when you close a workbook. This can be done either by specifying the state of the workbook Saved property, or by suppressing all alerts for the workbook.

How do I save and close a workbook in VBA?

Steps to Close a Workbook Specify the workbook that you want to close. Use the close method with that workbook. In the code method, specify if you want to save the file or not. In the end, mention the location path where you want to save the file before closing.


1 Answers

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.

like image 170
Motes Avatar answered Oct 05 '22 16:10

Motes