I have looked on here for some tips on getting rid of an excel instance after my program runs, but none of the suggestions seem to work. When i run it initially it will create an instance of excel, but while the program is still running and i rerun this code by clicking a button; it will create another instance of excel, but this time it removes the instance that it created leaving only the one that was created when the program was first initially ran.
What i have for code is this so far: (Updated code as of 9/14/2012)
Private Sub GetBatchFileContents()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim xlRan As Excel.Range
Dim xlVal(,) As Object
Dim lastRow As Int32
xlApp = New Excel.Application()
xlWB = xlApp.Workbooks.Open(TextBox1.Text.ToString(), _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing)
xlWS = xlWB.Worksheets.Item(1)
lastRow = xlWS.Cells(xlWS.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row
xlRan = xlWS.Range(xlWS.Cells(1, 1), xlWS.Cells(lastRow, 130))
xlVal = xlRan.Value2()
ReleaseObj(xlRan)
ReleaseObj(xlWS)
xlWB.Close(False, Type.Missing, Type.Missing)
ReleaseObj(xlWB)
xlApp.Quit()
ReleaseObj(xlApp)
End Sub
Private Sub ReleaseObj(ByRef obj As Object)
Try
Marshal.FinalReleaseComObject(obj)
Catch ex As Exception
Stop
Finally
obj = Nothing
End Try
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
End Sub
Thanks in advance for the feedback!