Skip to content

Excel Interop Programming Example Feedback #22316

Open
@JoeCEaton

Description

@JoeCEaton

The programming example is completely inadequate to demonstrate how to use interop with Excel. In your example, you skip the most difficult steps (saving and exiting). I suspect this is because the interface is problematic.

It's widely known that Excel simply won't exit properly if you do nearly anything useful. The "community hive mind" solution is to add a huge amount of extra code to cleanup:

if (excel != null)
{
	excel.DisplayAlerts = true;
        excel.Quit();
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excel);
        excel = null;
}

Why should I need to use marshaling to release this object? Why do I have to set the object to null? Why can't these cleanup chores be part of excel.Quit()? Why doesn't garbage collection deal with this when the application closes?

You don't just need to do that with the excel object either. You do this for every excel object you used: workbooks, worksheets, ranges, etc. I find myself writing no more than a line or two of code before I debug and check to see if there is an orphaned Excel app in task manager.

It would be nice if this worked better, but in it's current state, developing with Office interop is terribly expensive due to the time it takes to debug.

Even in your example, if you close the excel app without saving, you will be left with an orphaned excel app process in task manager.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions