Shezad Ahmed
asked on
Closing Excel correctly
Hi
I am having a problem that Excel is still running as a process.
I am having a problem that Excel is still running as a process.
Private Sub LOADEXCEL()
If IO.File.Exists(Fullpath) Then
Dim Proceed As Boolean = False
Dim xlApp As Excel.Application = Nothing
Dim xlWorkBooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim xlWorkSheet As Excel.Worksheet = Nothing
Dim xlWorkSheets As Excel.Sheets = Nothing
Dim xlCells As Excel.Range = Nothing
Dim supplierworksheetexists As Boolean = False
xlApp = New Excel.Application
xlApp.DisplayAlerts = False
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(Fullpath)
xlWorkSheets = xlWorkBook.Sheets
For Each xlWorkSheet In xlWorkBook.Sheets
Dim sheetname As String = xlWorkSheet.Name
loaddatatable(xlWorkSheet.Name)
'Only interested in first sheet
Exit For
Next
NAR(xlWorkSheets)
NAR(xlWorkSheet)
xlWorkBook.Close(False)
NAR(xlWorkBook)
NAR(xlWorkBooks)
xlApp.Quit()
NAR(xlApp)
Else
MessageBox.Show("'" & "" & "' not located. Try one of the write examples first.")
End If
End Sub
Private Sub loaddatatable(ByVal WorksheetName As String)
Try
Dim commandtext As String = "SELECT [OHOrderType] ,[OHSupplierAccountNumber],[OHSupplierOrderNumber],[OHOrderNumber],[OHOrderDate],[OHDueDate], " _
& " [OHNetOrderValue],[olItemDescription],[olQuantity],[olPrice],[olDiscountValue],[olTaxCode]" _
& " FROM [" + WorksheetName + "$]"
Using cn As New System.Data.OleDb.OleDbConnection
Using cmd As OleDbCommand = New OleDbCommand With
{
.Connection = cn,
.CommandText = commandtext
}
Dim FileName As String = Fullpath
Dim Builder As New OleDbConnectionStringBuilder With
{
.DataSource = FileName,
.Provider = "Microsoft.ACE.OLEDB.12.0"
}
Builder.Add("Extended Properties", "Excel 12.0; HDR=Yes;")
cn.ConnectionString = Builder.ConnectionString
cn.Open()
Trans_Table = New DataTable
Trans_Table.Load(cmd.ExecuteReader)
cn.Close()
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "loaddatatable()", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub NAR(ByVal o As Object)
Try
While (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
End While
Catch
Finally
o = Nothing
End Try
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER