Hi.. hope somebody can advise on what I might be doing wrong.. I've had a look on some other forums to no avail.
Basically, after I release my COM objects and close my excel application it's not closing. I know it's just a reference I'm missing somewhere or the order in releasing stuff but I can't figure it out and it's got me sighing now at 300SPM
The release object further down does this
Please helpCode:Public Sub releaseObject(ByVal obj As Object) Try ''''''''''''''''''''''''''''''''''''''''''' Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub
Code:'''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' Dim xlApp As excel.Application = Nothing Dim xlWorkBooks As excel.Workbooks = Nothing Dim xlWorkBook As excel.Workbook = Nothing Dim xlWorkSheets As excel.Sheets Dim xlWorkSheet As excel.Worksheet = Nothing xlApp = New excel.Application xlApp.ScreenUpdating = False xlWorkBooks = xlApp.Workbooks '''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' xlWorkBook = xlWorkBooks.Open(mysave) xlWorkSheets = xlWorkBook.Sheets xlWorkSheet = xlWorkSheets("tblFulfilment") 'Find last row of table for pivot cache Dim LastRow As Integer LastRow = xlWorkSheet.Range("A1000000").End(excel.XlDirection.xlUp).Row Dim rng As excel.Range rng = xlWorkSheet.Range("A1:AB" & LastRow) xlWorkSheet.Names.Add(Name:="RANGEFULFILMENT", RefersTo:=rng) ' create second sheet for pivot table If xlApp.Sheets.Count() < 10 Then xlWorkSheet = CType(xlWorkSheets.Add(), excel.Worksheet) Else xlWorkSheet = xlWorkSheets(2) End If xlWorkSheet.Name = "SUMMARY" ''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''' Progress("Creating Pivot Table") ''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ' specify first cell for pivot table on the second sheet Dim xlRange2 As excel.Range = CType(xlWorkSheet, excel.Worksheet).Range("A1") ' Create pivot cache and table Dim ptCache As excel.PivotCache = xlWorkBook.PivotCaches.Add(excel.XlPivotTableSourceType.xlDatabase, "RANGEFULFILMENT") Dim ptTable As excel.PivotTable = xlWorkSheet.PivotTables.Add(PivotCache:=ptCache, TableDestination:=xlRange2, TableName:="SUMMARY") Dim xlRowfield = excel.XlPivotFieldOrientation.xlRowField Dim xlDatafield = excel.XlPivotFieldOrientation.xlDataField Dim xlColumnfield = excel.XlPivotFieldOrientation.xlColumnField Dim xlPagefield = excel.XlPivotFieldOrientation.xlPageField Dim xlSum = excel.XlConsolidationFunction.xlSum Dim xlCount = excel.XlConsolidationFunction.xlCount Dim xlCompactRow = excel.XlLayoutRowType.xlCompactRow ' create Pivot Field, note that pivot field name is the same as column name in sheet 1 Dim FULFILMENT As excel.PivotField = ptTable.PivotFields("FULFILMENT") 'this formats the layout so rowfields go down the page instead of across xlWorkSheet.PivotTables("SUMMARY").RowAxisLayout(xlCompactRow) 'this line is needed to make all the datafields go across the columns instead of down ''''''''''''''''''''''''''''''''''''''''''' Progress("Adding Row Fields") ''''''''''''''''''''''''''''''''''''''''''' 'Adding row fields With FULFILMENT .Orientation = xlRowfield End With ''''''''''''''''''''''''''''''''''''''''''' Progress("Adding Data fields") ''''''''''''''''''''''''''''''''''''''''''' 'Adding Data fields With FULFILMENT .Orientation = xlDatafield .Function = xlCount End With '''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''' With ptTable ' Apply a style .TableStyle2 = "PivotStyleMedium2" ' Hide Field Headers .DisplayFieldCaptions = False End With 'hide pivottable box which usually shows on the right side of page xlWorkBook.ShowPivotTableFieldList = False xlWorkSheet.Range("A1").Select() '''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''' releaseObject(FULFILMENT) releaseObject(xlRowfield) releaseObject(xlDatafield) releaseObject(xlColumnfield) releaseObject(xlPagefield) releaseObject(xlSum) releaseObject(xlCount) releaseObject(xlCompactRow) releaseObject(ptTable) releaseObject(ptCache) releaseObject(xlRange2) releaseObject(rng) releaseObject(LastRow) ''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''' xlWorkBook.Save() xlApp.ScreenUpdating = True xlApp.Visible = True releaseObject(xlWorkSheet) releaseObject(xlWorkSheets) releaseObject(xlWorkBook) releaseObject(xlWorkBooks) xlApp.Quit() releaseObject(xlApp)





Reply With Quote
