Quote Originally Posted by jalzaaal
Hi all,

Is it possible to read an .xls file using vb unlike .txt file.

What i want to do is .....
i want to read the data of a cell of excel sheet and want to display it in a text box...... Is is possible?
Couple ways to do this. First, you can use the Excel automation objects to open the workbook and retrieve the value. The example uses early binding, but can be changed to late binding. This requires a reference to Excel:
VB Code:
  1. Dim oXL As Excel.Application, oBook As Excel.Workbook, oSheet As Excel.Worksheet, vValue As Variant
  2.  
  3. Set oXL = New Excel.Application
  4. Set oBook = oXL.Workbooks.Open("D:\Test.xls")
  5. Set oSheet = oBook.Worksheets("Sheet1")
  6.  
  7. vValue = oSheet.Cells(1, 1).Value       'Get the value from cell A1
  8. Debug.Print vValue
  9.  
  10. Set oSheet = Nothing
  11. oBook.Close
  12. Set oBook = Nothing
  13. oXL.Quit
  14. Set oXL = Nothing
Another way would be to use ADO to open the workbook into a recordset. This does not require a reference to Excel, but I'm not exactly sure if you need Excel installed on the machine to use Excel as a data provider. I don't have any machines without Excel to test this on. Maybe someone else could shed some light on this.
VB Code:
  1. Dim oRS As ADODB.Recordset, oConn As ADODB.Connection, sConString As String, sXLFile As String, vValue As Variant
  2.  
  3. sXLFile = "D:\Test.xls"
  4. sConString = "Provider= Microsoft.Jet.OLEDB.4.0;" & " Data Source=" & sXLFile & ";Extended Properties=Excel 8.0;"
  5. Set oConn = New ADODB.Connection
  6.  
  7. With oConn
  8.     .CursorLocation = adUseClient
  9.     .Open sConString
  10. End With
  11.  
  12. Set oRS = New ADODB.Recordset
  13.  
  14. With oRS
  15.     .CursorType = adOpenStatic
  16.     .CursorLocation = adUseClient
  17.     .LockType = adLockPessimistic
  18.     .Source = "SELECT * FROM [Sheet1$]"
  19.     .ActiveConnection = oConn
  20.     .Open
  21.     .MoveFirst
  22. End With
  23.  
  24. 'Work with the RS
  25.  
  26. oRS.Close
  27. oConn.Close
  28.  
  29. Set oRS = Nothing
  30. Set oConn = Nothing
You can read more about this method here.