Cannot close Excel.exe after Interop process
c#, excel-interop, winforms
Solution
Simple rule: avoid using double-dot-calling expressions, such as this:
var workbook = excel.Workbooks.Open(/*params*/)
...because in this way you create RCW objects not only for `workbook`, but for `Workbooks`, and you should release it too (which is not possible if a reference to the object is not maintained).
So, the right way will be:
var workbooks = excel.Workbooks;
var workbook = workbooks.Open(/*params*/)
//business logic here
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excel);
Problem
I'm having an issue with Excel Interop. The Excel.exe doesn't close even if when I realease instances. Here is my code : ``` using xl = Microsoft.Office.Interop.Excel; xl.Application excel = new xl.Application(); excel.Visible = true; excel.ScreenUpdating = false; if (wordFile.Contains(".csv") || wordFile.Contains(".xls")) { //typeExcel become a string of the document name string typeExcel = wordFile.ToString(); xl.Workbook workbook = excel.Workbooks.Open(typeExcel, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); object outputFileName = null; if (wordFile.Contains(".xls")) { outputFileName = wordFile.Replace(".xls", ".pdf"); } else if (wordFile.Contains(".csv")) { outputFileName = wordFile.Replace(".csv", ".pdf"); } workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFileName, XlFixedFormatQuality.xlQualityStandard, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); object saveChanges = xl.XlSaveAction.xlDoNotSaveChanges; ((xl._Workbook)workbook).Close(saveChanges, oMissing, oMissing); Marshal.ReleaseComObject(workbook); workbook = null; } ``` I saw that, with the `Marshal.RealeaseComObject` it should be work, but nothing. How can I fix this? Thank you.