469,600 Members | 2,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

To read an excel file with Excel Class in vb.net

I have added a reference Microsoft Excel 5.0 Object Library in my project.
the code is as follows:
Dim myExcel As Excel.Application ' Interface to Excel
Dim myWorkBookCollection As Excel.Workbooks ' Workbook-collection
Dim myWorkBook As Excel.Workbook ' Single Workbook (spreadsheet-collection)
Dim myWorkSheet As Excel.Worksheet ' Single spreadsheet

Dim fdlg As OpenFileDialog = New OpenFileDialog()
fdlg.Title = "VB.NET Corner Open File Dialog"
'fdlg.InitialDirectory = "c:\"
fdlg.Filter = "Excel Files (*.Xls)|*.Xls"
fdlg.FilterIndex = 2
fdlg.RestoreDirectory = True
If fdlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
txtFile.Text = fdlg.FileName
End If

myExcel = GetObject(, "Excel.Application")
If myExcel Is Nothing Then
MessageBox.Show("Could not load Excel.exe")
Exit Sub
End If
' initialise access to Excel's workbook collection
myExcel.Visible = True
myWorkBookCollection = myExcel.Workbooks
'open spreadsheet from disk

myWorkBook = myWorkBookCollection.Open(txtFile.Text, , False)
'get 1st sheet from workbook
myWorkSheet = myWorkBook.Sheets.Item(1)
'display the spreadsheet
'myExcel.Visible = True 'Read Cell A2
Dim myCell2 As Object = myWorkSheet.Range("A2", System.Reflection.Missing.Value)
Me.Text = myCell2.Value 'save and get out

now its giving error at this line
" myWorkBook = myWorkBookCollection.Open(txtFile.Text, , False)"

it gives Member Not Found. whereas .open is there in the list.
plz find the solution.
Jan 22 '08 #1
0 1165

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.