And the reason you didn't see the error is the "On Error Resume Next" at the
top of CommandButton2_Click(). It is the root of much evil. Get rid of it,
or at least reset the error handler immediately after.you've checked for the
error.
--
John Goodfellow
irtfnm
use john at goodfellowassoc dot com
wrote in message news:5252497@discussion.autodesk.com...
I have some code which should read an excel file. When I use the buttons my
Listbox remains empty. I have loaded the Excel Object Library. What is
wrong?
Private Sub CommandButton1_Click()
Dim excelApp As Excel.Application
Dim wbkObj As Workbook
Dim shtObj As WorkSheet
On Error Resume Next
UserForm1.Hide
Err.Clear
Set excelApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Err.Clear
Set excelApp = CreateObject("Excel.Application")
If Err <> 0 Then
MsgBox "Cannot start Excel", vbExclamation
End
End If
End If
excelApp.Visible = True
Set wbkObj = excelApp.Workbooks.Open("C:\Bob\Test.xls")
Set shtObj = excelApp.Worksheets(1)
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
Dim excelApp As Excel.Application
Dim wbkObj As Workbook
Dim shtObj As WorkSheet
Dim i As Integer
Dim txt As String
On Error Resume Next
UserForm1.Hide
Err.Clear
Set excelApp = GetObject(, "Excel.Appication")
excelApp.Visible = True
Set shtObj = excelApp.Worksheets(1)
ListBox1.Clear
i = 1
txt = shtObj.Cells(i, 2)
Do While txt <> ""
ListBox1.AddItem txt
i = i + 1
txt = shtObj.Cells(i, 2)
Loop
UserForm1.Show
End Sub