Beginner UserForm List Box Question

S

Shauna Koppang

Word 2000. If anyone know of a good place to look for this
kind of basic coding please let me know. I am a rank
beginner!

I have a templates in which I am trying to create a user
form that contains a couple of list boxes (could do combo
boxes) where the users would select an item in the list
and it would insert the results into a bookmarked area.

Have created the 2 Bookmarks on a single space (Text1 &
Text2), create a UserForm with 2 List Boxes and 1 Command
Button only on the form. Firstly, I can't figure out
where to enter the values that are to appear in a list box
and secondly, how to have it enter into the bookmark
position.

OK I have been able to use the ListBox1.AddItem to add
the items to the list when the command button is clicked
but it needs to be there to begin with, and then on the
click it needs to be placed at the bookmarks.

Coding for a straight text box I got working was:
Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("Text1").Range.InsertBefore TextBox1
'Check - if no contents give message
Sub ExitText1()
With ActiveDocument.FormFields("Text1")
If Len(.Result) = 0 Then
MsgBox "You must fill in this field"
End If
End With
End Sub

Sub GoBacktoText1()
ActiveDocument.Bookmarks("Text1").InsertBefore TextBox1
End Sub

.Bookmarks("Text2").Range.InsertBefore TextBox2
End With

UserForm1.Hide

End Sub

Your assistance would truly help. Thanks!!!
Shauna
 
S

Shauna Koppang

Hi Jonathan,

Thanks for this info. So I put the ListBox1.List = Array
statement into the AutoNew Macro which brings up the box
but the text does not show up.

Sub AutoNew()
'
' AutoNew Macro
' Macro created 29/07/2003 by Shauna Koppang
'
UserForm1.Show
ListBox1.List = Array("Zero", "One", "Two", "Three")
ListBox1.ListIndex = 0

End Sub

Is this what you mean about the UserForm_Initialize
statement? Sorry this is my first one and I am still a
little confused.

Her is the remainder of the code:

Private Sub CommandButton1_Click()
If ListBox1.ListIndex >= 0 Then
With ActiveDocument
.Bookmarks("Text1").Range.InsertBefore
ListBox1.Text
End With
Else
MsgBox "You must select an item first."
End If
End Sub

Private Sub ListBox1_Click()

End Sub

What am I still doing wrong and what book wuld you
recommend would be a good beginners (of Dummies Guide) to
learning UserForms and Word VBA Automations?

Thanks!
Shauna
 
J

Jonathan West

Shauna Koppang said:
Hi Jonathan,

Thanks for this info. So I put the ListBox1.List = Array
statement into the AutoNew Macro which brings up the box
but the text does not show up.

Sub AutoNew()
'
' AutoNew Macro
' Macro created 29/07/2003 by Shauna Koppang
'
UserForm1.Show
ListBox1.List = Array("Zero", "One", "Two", "Three")
ListBox1.ListIndex = 0

End Sub

Is this what you mean about the UserForm_Initialize
statement? Sorry this is my first one and I am still a
little confused.

No, that's not quite right.

With the UserForm displayed in the VBA editor, right-click on some empty
part of it and select "View Code". On the code window that appears, select
"UserForm" from the left-hand dropdown list above the main code area, and
then "Initialize" from the right-hand dropdown. The following code is
automatically inserted into the code pane

Private Sub UserForm_Initialize()

End Sub


You place your lines of code between them, like this

Private Sub UserForm_Initialize()

ListBox1.List = Array("Zero", "One", "Two", "Three")
ListBox1.ListIndex = 0
End Sub

Now, when you show the UserForm, the listbox will have the items in it, and
the first one will be selected.

Her is the remainder of the code:

Private Sub CommandButton1_Click()
If ListBox1.ListIndex >= 0 Then
With ActiveDocument
.Bookmarks("Text1").Range.InsertBefore
ListBox1.Text
End With
Else
MsgBox "You must select an item first."
End If
End Sub

Private Sub ListBox1_Click()

End Sub

What am I still doing wrong and what book wuld you
recommend would be a good beginners (of Dummies Guide) to
learning UserForms and Word VBA Automations?

That code should also be in the code window behind the UserForm.

You're nearly there. ListBoxes and Comboboxes are the only items where you
have to use code to set their contents. But *everything* can be set
dynamically in the UserForm if you need it to be, using exactly the same
technique as I have described for filling the listbox.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
S

Shauna Koppang

Hi Jonathan,

OK, tried your changes and the items display in the list
box with the first marque outlined in the list. But when
I select and item to click the command button to insert at
the bookmark, nothing happens. My bookmark is Text1... so
here is the code I am now trying. I really appreciate
your assistance during this maiden adventure.

Private Sub UserForm_Initialize()
ListBox1.List = Array("By Mail", "By Mail & Fax", "By
Mail & E-mail", "By Fax & E-mail", "By Hand", "By
Courier", "By Special Delivery", "By Registered Mail")
ListBox1.ListIndex = 0
End Sub
Private Sub CommandButton1_Click()

If ListBox1.ListIndex >= 0 Then
With ActiveDocument
.Bookmarks("Text1").Range.InsertBefore
ListBox1.Text
End With
Else
MsgBox "You must select an item first"
End If

End Sub

So how to I get my selection to display at the bookmark
when I click OK?

Thanks again!!!!
Shauna
 
J

Jonathan West

Hi Shauna,

Your code works perfectly for me. There is nothing wrong with it. Therefore,
something else must be wrong. There are three possibilities I can think of.

1. The bookmark is wrongly named, dpesn't exist or is in the wrong place

2. The listbox isn't actually called ListBox1.

3. The commandbutton isn't actually called CommandButton1.

Of the three, I suspect #3 is the most likely, as the others would be more
likely to give you an error message.


--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
S

Shauna Koppang

Hi Jonathan,

Checked out all you suggested below and was fine. I then
rechecked the settings for the list box and I had sent it
to another user here to look at as well and they had
changed it from single to multiple select and when I
changed it back, it worked. So perhaps that may have been
the issue all along.

THANK YOU SO MUCH!!!! for assisting me on this, my maiden
foray into list boxes. I truly appreciate all your
assistance. Now my task is to build a dialog box
combining list and text boxes to input the responses into
a template. May yet need your continued assistance.

Thanks again!!!

Shauna
 

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