problems using listbox.

D

dirk

I have put a listbox item on my word document and put the following code in
VB :



Private Sub Document_Open()

ListBox1.AddItem ("item 1"), 0

ListBox1.AddItem ("item 2"), 1

ListBox1.AddItem ("item 3"), 2

ListBox1.AddItem ("item 4"), 3



End Sub



Private Sub ListBox1_Click()

If ListBox1.ListIndex = 1 Then

MsgBox ("this is item 2"), vbOKOnly

If ListBox1.ListIndex = 3 Then

MsgBox ("this is item 4"), vbOKOnly



End If

End If

End Sub





My question : this code only works when I click item 2.

what is wrong please??
 
D

Doug Robbins

The End If's are out of place. The correct construction is:

If ListBox1.ListIndex = 1 Then
MsgBox ("this is item 2"), vbOKOnly
End If
If ListBox1.ListIndex = 3 Then
MsgBox ("this is item 4"), vbOKOnly
End If

or

If ListBox1.ListIndex = 1 Then
MsgBox ("this is item 2"), vbOKOnly
ElseIf ListBox1.ListIndex = 3 Then
MsgBox ("this is item 4"), vbOKOnly
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top