How to kill an Excel process?
-
Hello, I'm making a program that make use of Excel. In it, I create an Excel application and then make it visible, then perform several operations with it. Up to this point, everything OK. The problem comes when I try to close the Excel. At first I thougt that, being an object created within my app, the process will be destroyed by the garbage collector. Then I realized that not. I tried to use the ExcelApp.Quit() method, but it doesn't work either. In conclusion, I find myself with my program closed, but with a residual Excel process within an SVCHOST parent process I can't kill. Things turn to worse when I execute my app several times: I end up with several Excel processes, consuming no less than 8MB of memory each!!! I invoke the Excel with the following code: Imports Microsoft.Office.Interop.Excel ... Private Excel As New Microsoft.Office.Interop.Excel.Application() ' el Excel What can I do? Any help will be welcomed! Thanks in advance, Juan Pedro Pérez
-
Hello, I'm making a program that make use of Excel. In it, I create an Excel application and then make it visible, then perform several operations with it. Up to this point, everything OK. The problem comes when I try to close the Excel. At first I thougt that, being an object created within my app, the process will be destroyed by the garbage collector. Then I realized that not. I tried to use the ExcelApp.Quit() method, but it doesn't work either. In conclusion, I find myself with my program closed, but with a residual Excel process within an SVCHOST parent process I can't kill. Things turn to worse when I execute my app several times: I end up with several Excel processes, consuming no less than 8MB of memory each!!! I invoke the Excel with the following code: Imports Microsoft.Office.Interop.Excel ... Private Excel As New Microsoft.Office.Interop.Excel.Application() ' el Excel What can I do? Any help will be welcomed! Thanks in advance, Juan Pedro Pérez
Give this a try when you are done using Excel: ' Release Application object: If Not oExcel.UserControl Then oExcel.UserControl = True System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) 'Release worksheet: If Not oWorkSheet Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorkSheet) oWorkSheet = Nothing End If 'Release workbook: If Not oWorkBook Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorkBook) oWorkBook = Nothing End If oExcel = Nothing oExcel is your Excel application object. oWorksheet is your Excel worksheet object if you have one instantiated. oWorkbook is your Excel workbook object if you have one instantiated. Dean
-
Give this a try when you are done using Excel: ' Release Application object: If Not oExcel.UserControl Then oExcel.UserControl = True System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel) 'Release worksheet: If Not oWorkSheet Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorkSheet) oWorkSheet = Nothing End If 'Release workbook: If Not oWorkBook Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oWorkBook) oWorkBook = Nothing End If oExcel = Nothing oExcel is your Excel application object. oWorksheet is your Excel worksheet object if you have one instantiated. oWorkbook is your Excel workbook object if you have one instantiated. Dean
Thanks Dean, I'll try this and I'll tell you how it works. Juan Pedro Pérez