Excel automation application not closing after calling Quit

While working on creating a console application to manipulate an Excel file using COM interop, I ran into an issue where console application would not close even after I called Quit on the Application object. There was no exception or any error throw but the console application was still waiting for close.

The problem was that I had made a change in the worksheet but I did not save the workbook after it. Excel application sees that a workbook is in Dirty state so it wants to make sure that user of the application, which in my case is a console application, saves the unsaved changes before closing the workbook. There are few options you have here.

How to check if there are unsaved changes in Excel Workbook?

Workbook object exposes a property Saved that can be used to check if there are any unsaved changes. If value of this property is True that means you have unsaved changes.

if (myWorkbook.Saved)
{
 Console.Writeline("There are no unsaved changes to this workbook");
}

How to close Workbook without saving changes?

There may be cases where you have made some changes to some cells. That means Workbook is dirty now. But you really do not want to save the changes. If you do not take care of this, then your application will not close because Excel application instance is waiting for Workbook to be saved. You will use the same Saved property to deal with this. You can set this property to True to indicate that Workbook should be treated as saved and not prompted for saving.

if (!myWorkbook.Saved && !needToSave)
{
 myWorkbook.Saved = true;
}

This is all to how you can deal with saving or not saving of a dirty Excel Workbook.

Search

Social

Weather

-15.8 °C / 3.6 °F

weather conditions Clear

Monthly Posts

Blog Tags