Christof,
could you explain why the following code doesn't kill the Excel
process:
Application application = new ApplicationClass();
try {
Workbooks books = application.Workbooks;
Workbook book = books.Open(BookPath, false, true,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
application.DisplayAlerts = false;
XmlMaps maps = book.XmlMaps;
XmlMap dataMap = maps["Data_Map"];
dataMap.ImportXml(document.OuterXml, (object)true);
application.Calculate();
XmlMap outputMap = maps["Output_Map"];
outputMap.Export(outputPath, true);
Marshal.ReleaseComObject(outputMap);
outputMap = null;
Marshal.ReleaseComObject(dataMap);
dataMap = null;
Marshal.ReleaseComObject(maps);
maps = null;
Marshal.ReleaseComObject(book);
book = null;
Marshal.ReleaseComObject(books);
books = null;
}
finally {
application.Quit();
Marshal.ReleaseComObject(application);
application = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
I can't see any objects I've referenced that I haven't released.
Thanks,
Chris.
Christof said:
See Inline
When you use Application.Quit() on an Excel application,
there can still be an instance of Excel running,
as seen in Task Manager.
You can try following the advice on MSDN:
http://support.microsoft.com/kb/Q317109
but this didn't solve the problem for me.
It works, but you have to call ReleaseComObject on every!!! excel object
your referencing.
Even on collection.
So instead of
Excel.Workbook workbook = oApp.Workbooks[0]:
you've got to use
Excel.Workbooks workbooks = oApp.Workbooks;
Excel Workbook workbook = workbooks[0];
And then call ReleaseCommObject on workbooks as well.
Instead, I used a crow-bar and did the following:
foreach (Process process in Process.GetProcessesByName("Excel")) {
process.Kill();
}
That's Dangerous.
You can't be sure the user is not running another Excel-session.
Christof