dotnet / dotnet/docs

Excel Interop Programming Example Feedback

Open
#22,316 0 comments 0 reactions 0 assignees View on GitHub
:bomb: vanQUEST :mag: Needs Research :pushpin: seQUESTered advanced-concepts/subsvc dotnet-csharp/svc in-pr okr-quality
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

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.*

* ID: 5f0293f1-9cc7-1c81-351b-4f37784dd39d
* Version Independent ID: 5925edd7-d9bb-80d4-c722-922ef3cbc1a3
* Content: [Walkthrough: Office Programming (C# and Visual Basic)](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/walkthrough-office-programming)
* Content Source: [docs/csharp/programming-guide/interop/walkthrough-office-programming.md](https://github.com/dotnet/docs/blob/master/docs/csharp/programming-guide/interop/walkthrough-office-programming.md)
* Product: **dotnet-csharp**
* Technology: **csharp-advanced-concepts**
* GitHub Login: @BillWagner
* Microsoft Alias: **wiwagn**

---
[Associated WorkItem - 447342](https://dev.azure.com/msft-skilling/Content/_workitems/edit/447342)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.