Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 I'm using:

    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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...