3

I understand that I should release COM objects when using interop. Are things a bit different when developing and Add-In, Excel for example? Here is a loop I have and I was curious to know if the Marshal.ReleaseComObject is necessary?

 foreach (var sheet in results.Sheets)
            {
                var newSheet = workbook.AddSheet();

                newSheet.SetSheetTabColor(sheet.TabColor);
                newSheet.SetSheetName(sheet.TabName);
                newSheet.Cells.SetFont("Calibri", 8);
                newSheet.FreezeRow(1);

                var endRow = sheet.Data.GetUpperBound(0) + 1;
                var endColumn = sheet.Data.GetUpperBound(1) + 1;

                var writeRange = newSheet.SetWriteRange(1, 1, endRow, endColumn);



                writeRange.Value2 = sheet.Data;

                newSheet.AutoFitColumns();
                newSheet.RemoveColumn(1);

                Marshal.ReleaseComObject(newSheet);

            }

Also, I created a library with extension methods. One example is workbook.AddSheet() AddSheet looks like this:

public static Worksheet AddSheet(this Microsoft.Office.Interop.Excel.Workbook workbook)
        {
            var sheets = workbook.Sheets;

            return sheets.Add(After: sheets[sheets.Count]);
        }

Since I am accessing sheets from workbook.Sheets, do I have to release of this object? If so where since I am returning a Worksheet? I can't release before I return?

This may be a dumb question, but if the Marshal.ReleaseComObject was not necessary in the foreach scope, does it hurt even if it is still there?

xaisoft
  • 3,343
  • 8
  • 44
  • 72

1 Answers1

2

Marshal.ReleaseComObject is used only if you need to control the lifetime of an COM object in timely manner, or in specific order. For casual usage of COM objects i would not advice you to use this method at all.

Check here

This method is used to explicitly control the lifetime of a COM object used from managed code. You should use this method to free the underlying COM object that holds references to resources in a timely manner or when objects must be freed in a specific order.

About your second question

Since I am accessing sheets from workbook.Sheets, do I have to release of this object? If so where since I am returning a Worksheet? I can't release before I return?

The COM implementation in .NET is using reference counting mechanism to detect if object is used or not, so NO you don't have to release anything explicitly. The framework is resposible for this.

NOTE: Use the approriate .Close(for Workbook), .Quit(for Application) methods for proper release of resurces. When you use .Quit() over the application object you will close the excel process in windows (so this will release all resource), and .Close() over a Workbook to release file locks .. etc over specific excel file.

vasil oreshenski
  • 2,788
  • 1
  • 14
  • 21
  • In my case, the user opens excel first and runs an addin on the existing file, but they usually don't close it until they want to, so in this case, I don't have to call close or quit, correct? – xaisoft Dec 21 '17 at 15:12
  • Correct, in addin scenarios this is not neceserry. The user will close the application. – vasil oreshenski Dec 21 '17 at 15:18