Microsoft Office Interop Excel Application

Jul 20, 2015  To change the default and use PIAs instead of embedding type information, expand the References node in Solution Explorer and then select Microsoft.Office.Interop.Excel or Microsoft.Office.Interop.Word. If you cannot see the Properties window, press F4. Find Embed Interop Types in the list of properties, and change its value to False. This is a widely-spread issue with Office applications. All Excel add-ins/automation applications should systematically release their references to Excel objects when they are no longer needed. Failing to systematically release reference to Excel objects can prevent Microsoft Office Excel.

Active3 years, 2 months ago

The following code leaves a Microsoft Excel background process running, until after my program has exited:

Why? What am I missing?

pnuts
51k7 gold badges65 silver badges105 bronze badges
Exampleskinnysoftwareskinnysoftware
5391 gold badge6 silver badges12 bronze badges

3 Answers

Got it!

application.Workbooks != application.Workbooks

This property doesn't expose a variable, it generates a value. So every time I access the Workbooks property I create a new COM object.

I fixed the code and all is well. Thanks, everybody.

skinnysoftwareskinnysoftware
5391 gold badge6 silver badges12 bronze badges

This is a widely-spread issue with Office applications. All Excel add-ins/automation applications should systematically release their references to Excel objects when they are no longer needed. Failing to systematically release reference to Excel objects can prevent Microsoft Office Excel from shutting down properly. See Systematically Releasing Objects for more information. It is related to Outlook, but the same principles can be applied to all Office applications.

Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Excel object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.

Eugene AstafievEugene Astafiev
20.4k2 gold badges11 silver badges28 bronze badges

THIS IS WRONG WAY TO DO LIKE THIS, but this is most easy way to fix the issue:

all you need is to init all uninitialized variables when you need to work with it, and call CloseExcelApp() when you need to close app.

Andrew

Microsoft.office.interop.excel.application Error

Andrew
2,9201 gold badge16 silver badges32 bronze badges

Microsoft.office.interop.excel.application Dll

Not the answer you're looking for? Browse other questions tagged c#exceloffice-interopexcel-interop or ask your own question.