I'm writing an application, which accesses Excel file with this code:
RBTApplication = new Excel.Application();
RBTWorkbooks = RBTApplication.Workbooks;
RBTWorkbook = RBTApplication.Workbooks.Open(
RBTDataSet.Instance.FilePath, 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t", true, false, 0, true, 1, 0 );
RBTWorksheet = (Excel.Worksheet)RBTWorkbook.Worksheets.get_Item( 1 );
RBTRange = RBTWorksheet.UsedRange;
After I load data that I need, I try to close Excel application with this piece of code:
RBTWorkbook.Close();
RBTApplication.Quit();
RBTWorksheet.ReleaseComObject();
RBTWorkbook.ReleaseComObject();
RBTWorkbooks.ReleaseComObject();
RBTApplication.ReleaseComObject();
My problem is that EXCEL.EXE process is still running. Simple solution like killing all Excel processes doesn't sound great, as my process can be one of many spreadsheets used at the same time. This one also affects opening new excel spreadsheets - every opens as read only until I don't kill this process.
So ... how to close this application properly OR kill this specific process (is there any way to get PID from COM object ?). I think I've found one (haven't tried) - get list of excell processes before and after opening new ExcellApp, compare lists (get difference), kill specific process knowing its PID. But this solution doesn't seem like a proper way of handling COM objects and other applications from within my app.