Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Way to Release Excel Interop com Object
    primarykey
    data
    text
    <p>Experts, Please let me know the best way I can do this...</p> <p>I am working on a VB.Net application that uses the Microsoft.Office.Interop.Excel Object Library to create worksheets within a workbook and Pivot Tables within those worksheets.</p> <p>My code looks something like this:</p> <pre><code>Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application Dim wbk As Microsoft.Office.Interop.Excel.Workbook = Nothing Dim wksRawData As Microsoft.Office.Interop.Excel.Worksheet = Nothing Dim wksPvtTbl As Microsoft.Office.Interop.Excel.Worksheet = Nothing Dim pvtCache As Microsoft.Office.Interop.Excel.PivotCache = Nothing Dim pvtTables As Microsoft.Office.Interop.Excel.PivotTables = Nothing Dim pvtTable As Microsoft.Office.Interop.Excel.PivotTable = Nothing Dim r1 As Microsoft.Office.Interop.Excel.PivotField = Nothing Dim r2 As Microsoft.Office.Interop.Excel.PivotField = Nothing Dim df1 As Microsoft.Office.Interop.Excel.PivotField = Nothing Try ... Create the objects, put in the information Catch ex As Exception MessageBox.Show("There was an error creating the Excel file", "Error Creating File", MessageBoxButtons.OK) Finally ReleaseObject(r1) ReleaseObject(r2) ReleaseObject(df1) ReleaseObject(pvtTable) ReleaseObject(pvtTables) ReleaseObject(pvtCache) ReleaseObject(wksRawData) ReleaseObject(wksPvtTbl) ReleaseObject(wbk) ExcelApp.DisplayAlerts = True ExcelApp.Quit() ReleaseObject(ExcelApp) ExcelApp = Nothing wbk = Nothing wksRawData = Nothing GC.Collect() End Try </code></pre> <p>Then I have code that looks as follows for my release objects:</p> <pre><code>Public Sub ReleaseObject(ByRef Reference As Microsoft.Office.Interop.Excel.Application) Dim i As Integer If Reference IsNot Nothing Then i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference) While i &gt; 0 i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference) End While Reference = Nothing End If End Sub Public Sub ReleaseObject(ByRef Reference As Microsoft.Office.Interop.Excel.Workbook) Dim i As Integer If Reference IsNot Nothing Then i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference) While i &gt; 0 i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference) End While Reference = Nothing End If End Sub ... etc </code></pre> <p>I know there are MANY solutions out there for this kind of problem, but I’m getting lost in all the different ones to know what best fits my current situation… Is this a good way to do this and, if not, what is a more efficient way??</p> <p>Thanks!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload