Clean up after opening excel from C#/.NET application(excel.exe process not terminating)

varghesejim

New Member
Joined
Oct 7, 2014
Messages
2
We are opening an excel file from our application and display that to the user

Code:
            Microsoft.Office.Interop.Excel.Application ExcelApp;
            try
            {
                ExcelApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
            }
            catch
            {
                ExcelApp = return new Microsoft.Office.Interop.Excel.Application();
            }

            ExcelApp.Visible = true;
            ExcelApp.WindowState = XlWindowState.xlMaximized; 

            var wrkBooks = ExcelApp.Workbooks;

            var wrkBook = wrkBooks.Open(fileName, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
                                         true, false, 0, true, false, false);

This works and opens the excel file for the user. However, when the user closes the excel file, excel.exe still remains in memory.

Probably the application still has some references to excel. How to make sure we remove all references to excel from the .net app?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
AFAIK, you need to call ReleaseCOMObject and garbage collect, preferably twice.
 
Upvote 0
AFAIK, you need to call ReleaseCOMObject and garbage collect, preferably twice.

Thanks.Added the following after the code. But this does not have any impact it seems.After closing excel, the process still remains in memory.

Actually, excel.exe remains in memory even after closing the .NET application.

Code:
  GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();


                    Marshal.ReleaseComObject(wrkBooks);
                    Marshal.ReleaseComObject(wrkBook);
 
Upvote 0
I think you want to release the com objects first, and you forgot to release excelApp.
 
Upvote 0
Here is an example of how I did this using VB.Net. It may help you with your C# version.
I have removed the error trapping to reduce the amount of code in this reply.
Excel was always released without any problems.
Code:
    Public Sub Main()
        RunXYZ()
    End Sub
    Sub RunXYZ()
        Const strWorkbookName As String = "C:\Documents\XYZ.xlsm"
        Dim oExcel As Microsoft.Office.Interop.Excel.Application
        Dim oBook As Microsoft.Office.Interop.Excel.Workbook
        Dim oBooks As Microsoft.Office.Interop.Excel.Workbooks
        Dim strFilenameCheck As String
        Dim strMacroName As String
	'
            strMacroName = "RunXYZ"
            oExcel = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
            oExcel.Visible = False
            oBooks = CType(oExcel.Workbooks(), Microsoft.Office.Interop.Excel.Workbooks)
            strFilenameCheck = Dir(strWorkbookName)
            If strFilenameCheck <> "" Then
                oBook = CType(oBooks.Open(strWorkbookName), Microsoft.Office.Interop.Excel.Workbook)
                oExcel.DisplayAlerts = False
                oExcel.Run(strMacroName)
                oExcel.DisplayAlerts = True
            End If
	'
            oBook.Close(False)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
            oBook = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
            oBooks = Nothing
            oExcel.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
            oExcel = Nothing
            GC.Collect()
    End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,688
Messages
6,186,443
Members
453,355
Latest member
Shaz_7

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top