Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot close Excel.exe after Interop process

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.

like image 703
provençal le breton Avatar asked Jun 28 '13 14:06

provençal le breton


People also ask

How to close Excel process in c#?

workbook. Close(0); excelApp. Quit();


1 Answers

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); 
like image 189
Dzmitry Martavoi Avatar answered Sep 19 '22 23:09

Dzmitry Martavoi