Excel crash after opening it into vb.net using introp
Followig is my code which i used for importing the data from excel
Dim misValue As System.Reflection.Missing
Dim tbl As New System.Data.DataTable
Dim xlApp As nExcel.Application
Dim xlWorkBook As nExcel.Workbook
Dim xlWorkSheet As nExcel.Worksheet
xlApp = New nExcel.Application
Try
xlWorkbook = xlApp.Workbooks.Open(sFilePath)
Dim issheetext As Boolean
Dim sheet As nExcel.Worksheet
For Each sheet In xlWorkbook.Sheets
If sheet.Name.Equals(sSheetname) Then
issheetext = True
Exit For
End If
Next
If issheetext Then
xlWorkSheet = DirectCast(xlWorkbook.Sheets(sSheetname), nExcel.Worksheet)
' get range object
Dim myRange As nExcel.Range = DirectCast(xlWorkSheet.get_Range("A1:BZ102451"), nExcel.Range)
For i As Integer = 0 To myRange.Columns.Count
Dim cellval As Object
cellval = DirectCast(myRange.Cells(1, i + 1), Object)
tbl.Columns.Add(cellval.ToString(), cellval.GetType())
Next
For i As Integer = 0 To myRange.Rows.Count - 2
tbl.Rows.Add()
For j As Integer = 0 To myRange.Columns.Count - 1
Dim CellValue As Object = DirectCast(myRange.Cells(2 + i, j + 1).Value2, Object)
tbl.Rows(i)(j) = CellValue
Next
Next
End If
Catch ex As Exception
Finally
Try
releaseObject(xlWorkSheet)
releaseObject(xlWorkbook)
releaseObject(xlApp)
Catch
End Try
End Try
Return tbl
but when debugger cross xlWorkbook = xlApp.Workbooks.Open(sFilePath)
its shows me
and in exception it shows me
The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
Thanks in Advance
Couple of things based on your code I believe your code is correct:
User contributions licensed under CC BY-SA 3.0