7

I am working with Excel Interop COM object. I am writing a method in which I am opening and closing an Excel application followed by opening an Excel workbook and sheet. After I'm done working on them I am closing both the application and workbook. My problem is that this method can be called repeatedly several times and when the sheet and app are closing I get from the Excel application a message if I want to save the changes I've made. The method get stuck until I press "No" but I can't tell the user to press it every time.

How can I close the application without receiving this message or at least answer it by code?

These are the methods I'm using to close the application and workbook:

private void FreeWorkSheetResources(Excel.Worksheet sheet)
{
  Marshal.ReleaseComObject(sheet);
  sheet = null;
}

private void FreeWorkBookResources()
{
  if (_excelSheets != null)
  {
    Marshal.ReleaseComObject(_excelSheets);
    _excelSheets = null;
  }
  _excelWorkBook.Close();
  Marshal.ReleaseComObject(_excelWorkBook);
  _excelWorkBook = null;
}

private void FreeApplicationResources()
{
  _app.Quit();
  Marshal.ReleaseComObject(_app);
  _app = null;
  GC.Collect();
}

And this is the way I'm using them:

if (_app == null)
  {
    _app = new Excel.Application();
  }
  if (_excelWorkBook == null)
  {
    _excelWorkBook = GetWorkBook(_filePath);
  }
  ....
  ....
  FreeWorkBookResources();
  FreeApplicationResources();
nvoigt
  • 75,013
  • 26
  • 93
  • 142
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • so you are not saving the changes which you are making on worksheet right? – Sudhakar Tillapudi Nov 14 '13 at 12:18
  • I am only reading from the worksheet, not updating it. I don't know why he's even asking me if I want to save any changes – CodeMonkey Nov 14 '13 at 12:19
  • Duplicate of [Trying to exit C# Excel Workbook without a dialog box](http://stackoverflow.com/questions/17413887/trying-to-exit-c-sharp-excel-workbook-without-a-dialog-box). Please use the search. – CodeCaster Nov 14 '13 at 12:20

5 Answers5

14

While Closing Your Excel WorkBook you can use following Statements:

object misValue = System.Reflection.Missing.Value;
xlWorkBook.Close(false, misValue, misValue);
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • 1
    You actually do not even need to create a variable. The following works for me: xlWorkBook.Close(false, Missing.Value, Missing.Value); – Gerhard Powell Jan 12 '16 at 14:02
5

My problem was similar. I needed to generate Excel Sheet, then convert it to PDF. My problem was that the Excel App was displaying and notifying me to save before Exit. The solution was setting .Visible = flase and .DisplayAlerts = False

Thanks to @adil

shA.t
  • 16,580
  • 5
  • 54
  • 111
Cogent
  • 404
  • 7
  • 16
3

Try this:

excelApp.DisplayAlerts = False;
//save and close your workbook
excelApp.DisplayAlerts = True;
adil
  • 31
  • 3
2

Do this when closing workbook.

_excelWorkBook.Close(true);
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
1

I had this issue where the save dialog box was still displaying after opening and reading an Excel file without edit.

Setting

xlApplication.Visible = false;
xlApplication.DisplayAlerts = false;

and closing the workbook with the SaveChanges parameter as false

xlWorkbook.Close(false, Missing.Value, Missing.Value);

didn't work either. I was working with an Excel file type of .xlsb (binary with macros), when I saved as .xlsx (without macros) the .Close method worked. Saving as .xslm (xslx with macros) gave me the same problem of the save dialog displaying.

I turned to the MS support site on VBA for Excel and found this article, https://support.microsoft.com/en-us/help/213428/how-to-suppress-save-changes-prompt-when-you-close-a-workbook-in-excel detailing how to accomplish the close without save in a VBA macro. Adapting to C# I explicitly set the Saved property to true. The below worked for me using .xlsx, .xlsm, .xlsb file types.

        Excel.Application xlApp = new Excel.Application();
        xlApp.DisplayAlerts = false;
        xlApp.Visible = false;
        var xlWorkbook = xlApp.Workbooks.Open(fileInfo.FullName);

        // do work

        xlWorkbook.Saved = true;
        xlWorkbook.Close(false, Missing.Value, Missing.Value);
        xlApp.Quit();
Kacey
  • 89
  • 1
  • 6