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



## varghesejim (Oct 7, 2014)

We are opening an excel file from our application and display that to the user


```
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?


----------



## RoryA (Oct 7, 2014)

AFAIK, you need to call ReleaseCOMObject and garbage collect, preferably twice.


----------



## varghesejim (Oct 7, 2014)

RoryA said:


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


```
GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();


                    Marshal.ReleaseComObject(wrkBooks);
                    Marshal.ReleaseComObject(wrkBook);
```


----------



## RoryA (Oct 7, 2014)

I think you want to release the com objects first, and you forgot to release excelApp.


----------



## Derek Brown (Oct 7, 2014)

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.

```
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
```


----------

