Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox in MS Word

Status
Not open for further replies.

jfhewitt

Technical User
Jun 6, 2000
287
US
I guess I'm spoiled by Access, but I can't figure out how to insert a listbox into a Word macro to enable the user to select text to be inserted into a document. Everything I've read leaves me very confused. Can anyone help a senile old man do this?? Thanks.
 
You can insert a User Form into your project, then add the listbox to the form. Then, from your macro code, show the form (UserForm1.Show).
 
I've done that several times, but all I get is an empty listbox. Since there is no wizard like Access, how do I get the data into the listbox, and how do I then insert the selected data into the document. Is there a VBA procedure that can be inserted into the macro to create and fill a listbox?? This seems like it should be a very simple procedure, but it sure is confusing. I can get as far as inserting a UserForm, and adding the listbox, but where do I go from there? Thanks.
 
In your main module, insert this code:
Code:
Option Explicit

Public Sub TestListBox()
    UserForm1.Show
End Sub
Add a user form (UserForm1) and put this code in its code window:
Code:
Option Explicit

Private Sub CommandButton1_Click()
    Dim iIndex As Integer
    Dim sValue As String
    Dim sMsg As String
    
    iIndex = ListBox1.ListIndex
    sValue = ListBox1.Text
    
    sMsg = "Index: " & iIndex & vbCrLf & _
           "Value: " & sValue
    MsgBox sMsg
    
    Unload Me
    
End Sub

Private Sub UserForm_Activate()
    Dim i As Integer
    'Get this data from your source
    Dim TmpData(5) As String
    TmpData(0) = "One"
    TmpData(1) = "Two"
    TmpData(2) = "Three"
    TmpData(3) = "Four"
    TmpData(4) = "Five"
    
    For i = LBound(TmpData) To UBound(TmpData)
        ListBox1.AddItem TmpData(i)
    Next i
End Sub
Switch back to the document and run the TestListBox macro.
This should get you started. Hope it helps!
 
Thanks...I did get the example in Word Macros and VBA handbook to work, but my simpler test macro using just a listbox seems to hang up on the variable. Here is my code:

Sub AATestMacro()
'
Load frmListBoxTest
frmListBoxTest.Show
End Sub

Private Sub cmdOK_Click()
Unload frmListBoxTest
var1$ = lstColorBox.Value
Selection.TypeText Text:=var1$
End Sub

Private Sub UserForm_Initialize()
lstColorBox.AddItem "Red"
lstColorBox.AddItem "White"
lstColorBox.AddItem "Blue"
End Sub

After initializing the form data, when I debug it loads a null into the variable var1$. Sometimes the "lstColorBox.Value is "red" or whatever I select...other times its a null. What is confusing is the procedure works OK on the example from the manual, but seems to hang up on my simpler test. Any ideas??
You're suggestion looks great, but I would rather stick with the simple method, if possible. TIA
 
Got it...the Unload statement in the OK button was messing it up...although it worked OK on the manual sample. When all is said and done, its a fairly straightforward method of creating a list box. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top