Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Excel VB.NET
Hello,
 
I am trying to read an excel file in my vb windows application.This is my code for reading Excel
 
Imports System.IO
Imports System.Runtime.InteropServices
 
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xlApp As Excel.Application  
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim range As Excel.Range
        Dim rCnt As Integer
        Dim cCnt As Integer
        Dim Obj As Object
        xlApp = New Excel.ApplicationClass()
               xlWorkBook = xlApp.Workbooks.Open("C:\StatusReport.xls")
               xlWorkSheet = xlWorkBook.Worksheets("sheet1")
        range = xlWorkSheet.UsedRange
        For rCnt = 1 To range.Rows.Count
            For cCnt = 1 To range.Columns.Count
                Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
                MsgBox(Obj.value)
            Next
        Next
        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
 
 

When I am executing this , getting an exception
 

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ExceltoHtml1.exe
 
Additional information: COM object with CLSID {00024500-0000-0000-C000-000000000046} is either not valid or not registered.
at line
xlApp = New Excel.ApplicationClass()
 
I searched in Google but dint get any usefull info.
 
Any help will be appriciated.
Thanks.
Posted 14-Dec-12 0:34am
Comments
01010RAJ at 14-Dec-12 5:38am
   
check for rows,greater than 65536 or not?
if greater than 65536,so u may got this error!
Naveen_143 at 14-Dec-12 5:51am
   
Hi Raj..
I am having only 10 rows in my file and more over exception is coming before accessing the file.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Replace:
xlApp = New Excel.ApplicationClass()
with
xlApp = New Excel.Application()
and everything should be OK.
 
BTW: ApplicationClass is used with C#.
  Permalink  
Comments
Naveen_143 at 22-Feb-13 4:48am
   
thanks for ur response..
Maciej Los at 22-Feb-13 14:24pm
   
You're welcome.
If your problem was solved, please mark this answer as solved and rate it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 DamithSL 225
2 Maciej Los 210
3 Richard MacCutchan 178
4 BillWoodruff 155
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100