how to populate a listbox in word vba

G

Go

Hi,

How do i get a list of names (I have the five names I need) to appear on a
list box on an word vba form. I have created the vba form and listbox1 but I
can't see how to get the names on there?

Thanks in advance
Go
 
D

Doug Robbins

This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

Private Sub UserForm_Initialize()
Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range,
m As Long, n As Long
' Modify the path in the following line so that it matches where you
saved Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.


--
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
 
T

technogenii

Is there a way to do this without creating an external file? I'd like to put
the options right into the code so that only one word file needs to be
distributed.
 
G

Greg

tg,

Do it in the userform initialize event. Here are a couple of examples:

Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "Item1"
.AddItem "Item2"
.AddItem "Item3"
End With
'or
Dim i As Long
Dim myArray As Variant
myArray = Split("Item1 Item2 Item3", " ")
For i = 0 To UBound(myArray)
Me.ListBox2.AddItem myArray(i)
Next i
End Sub
 
D

Doug Robbins

Having the items in an external table does make it very easy to maintain the
list. If that's not an issue, then use the .AddItem method in the
Initialize event as Greg suggests.

--
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
 
A

Anne P.

You could also store them as AutoText Entries and load the list box from
there. For example, in Word's autotext list there is a category called
"Closing" which stores items like: Very truly yours, etc. These are saved
in the Normal Template. So you could use code like this to load a listbox
named lstClosings with these autotext entries:

Dim objATE As AutoTextEntry

For Each objATE In NormalTemplate.AutoTextEntries
If objATE.StyleName = "Closing" Then
lstClosing.AddItem objATE.Name
End If
Next objATE

Hope this helps,
Anne P.
 

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