Opening Excel file in VB.Net

NekoManu

Well-known member
Joined
Jul 23, 2004
Messages
75
Location
Belgium
I have never tried this before, so I have been looking for a workng example. So far no luck.

I want to open an Excel 2007 file in VB.Net. When I try I get an error: Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

I have no idea what is going on.

This is the code Im using:
Code:
    Private Sub btnExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Open("C:\Test.xlsx")
        xlWorkSheet = xlWorkBook.Worksheets("sheet1")
        display the cells value B2
        MsgBox(xlWorkSheet.Cells(2, 2).value)
        edit the cell with new value
        xlWorkSheet.Cells(2, 2) = "http://vb.net-informations.com"
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)
    End Sub
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
 
Back
Top