DotNet only frees a COM object after all descriptors have been released. What I do is comment on everything, and then add the part. See if it releases Excel. If it does not comply with the following rules. When it is released, add more code until it is released again.
1) When you create Excel variables, set all values ββto null (this avoids the errors caused by the error)
2) Do not reuse variables without releasing them first Marshal.FinalReleaseComObject
3) Do not double the point (ab = z) . dotNet creates a temporary variable that will not be released.
c = ab; c = z; Marshal.FinalReleaseComObject(c);
4) Release all excel variables. The faster the better.
5) Set it back to NULL.
Set the culture to "en-US". There is a bug that breaks Excel with some cultures. This ensures that it will not.
Here is an idea of ββhow your code should be structured:
thisThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); InteropExcel.Application excelApp = null; InteropExcel.Workbooks wkbks = null; InteropExcel.Workbook wkbk = null; try { excelApp = new InteropExcel.Application(); wkbks = excelApp.Workbooks; wkbk = wkbks.Open(fileName); ... } catch (Exception ex) { } if (wkbk != null) { excelApp.DisplayAlerts = false; wkbk.Close(false); Marshal.FinalReleaseComObject(wkbk); wkbk = null; } if (wkbks != null) { wkbks.Close(); Marshal.FinalReleaseComObject(wkbks); wkbks = null; } if (excelApp != null) {
Gerhard powder
source share