MS Excel : V2010 : Not Releasing Object : Excel Stays on Task Manager

  • Thread starter Thread starter Bre-x
  • Start date Start date
B

Bre-x

Guest
I modify my code with (Release Excel File/Application post extraction of Data)

But it is not working. I am not doing it right.

Please help

Dim the_path As String = app_path & curr_mach & "\" & curr_cust
Dim the_file As String = app_path & curr_mach & "\" & curr_cust & "\" & curr_mach & curr_prog

Dim xlApp As Microsoft.Office.Interop.Excel.Application = Nothing
Dim xlWorkBooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing

If IO.Directory.Exists(the_path) = False Then
MsgBox("Directory does not exist : " & the_path, MsgBoxStyle.Exclamation, app_cia)
Else
If IO.File.Exists(the_file & ".xls") = False Then
MsgBox("Unable to find File : " & the_file & ".xls", MsgBoxStyle.Exclamation, app_cia)
Else
xlApp = New Microsoft.Office.Interop.Excel.Application
xlApp.DisplayAlerts = False
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(the_file & ".xls")

Try
Cursor.Current = Cursors.WaitCursor
xlApp.Visible = False
xlWorkSheet = xlWorkBook.Worksheets("TS")

With xlWorkSheet
.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, the_file & "_TS.pdf")
End With

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
xlWorkSheet = Nothing

Catch ex As Exception
MsgBox("excel_pdf : " & Err.Description, MsgBoxStyle.Critical, app_cia)
Finally
Cursor.Current = Cursors.Default
xlWorkBook.Close()
xlApp.UserControl = True
xlApp.Quit()
Release(xlWorkSheet)
Release(xlWorkBook)
Release(xlWorkBooks)
Release(xlApp)
Process.Start(the_file & "_TS.pdf")
End Try
End If
End If

Thank you All,

John


Private Sub Release(ByVal sender As Object)
Try
If sender IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(sender)
sender = Nothing
End If
Catch ex As Exception
sender = Nothing
End Try
End Sub

Continue reading...
 
Back
Top