Userforms and VBA in Word 2007/2010 - Combobox with addresses

Anonymous
2012-02-09T23:44:33+00:00

Hello,

I've been trying to get used to working with User forms, and I've gone as far as being able to create a form, but I am uncertain how to code it to work with a template.  I'm still waiting for some books to come in that I ordered to help me out, so in the mean time I'm looking for a little assistance from you amazing people :-)

The main thing I'm trying to work on is creating a combobox so that I can have a very long list that will continue to grow as I obtain more contacts.  I wanted to use dropdown form fields but realize I can't exceed 25 items per dropdown field.  I could use several dropdown fields to call the data from another document, but I'd like things to look clean.  So I think a combobox will be a much better option.  I want the combo box to have the names of all my contacts, and once a contact is selected, I want their entire mailing address to appear at the top of my letter template.

So let's say I select 'Option1' in the combo box, I'd like the top of my letter to show something resembling the following:

Recipient Name

Address

City, State/Province

Zip Code/Postal Code

I hope I've explained what I'd like done clearly enough, but if you require further explanation I'd be happy to provide.  If there's a better way to do this I'd be happy to hear it.

Thank you!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2012-02-10T07:04:41+00:00

The simplest solution, if you have it, is to use Outlook to both store the addresses and provide an address book dialog to insert the addresses where you require them. This is covered at http://www.gmayor.com/Macrobutton.htm.

If you want to list the addresses on a userform you will need a multi-column list box and somewhere to store the addresses - probably a document containing a table or perhaps an Excel worksheet, which can be read into the list box. Take a look at http://gregmaxey.mvps.org/word_tip_pages/Old_Tip_Pages/Populate_UserForm_ListBox.htm.

You can then populate a document variable with the contents of the selected item and use a docvariable field to reproduce the value from the docvariable in the document.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
Anonymous
2012-02-16T08:44:29+00:00

Don't use bookmarks with a protected form, use a docvariable field in the document and write the value of the selected item from the list box to the variable. Then the protection becomes irrelevant.

If using a multi-column list box, then you will need to re compile the address from the list box columns in the format you require into the variable. Something like the following. I have set the listbox to display only the first column to avoid clutter.

Insert { DocVariable Address } into the document at the place you want the address to appear.

Option Explicit

Private oVars As Variables

Private oSource As Document

Private oTable As Table

Private oRng As Range

Private strWidth As String

Private i As Long, j As Long, k As Long

Private m As Long, n As Long

Private Sub UserForm_Initialize()

Set oSource = Documents.Open(FileName:="C:\Addresses.doc", _

                             AddToRecentFiles:=False, _

                             Visible:=False)

Set oTable = oSource.Tables(1)

'Get the number of list members (i.e., table rows - 1 if header row is used)

i = oTable.Rows.Count - 1

'Get the number of list member attributes (i.e., table columns)

j = oTable.Columns.Count

'Set the number of columns in the Listbox

With Me.ListBox1

    .ColumnCount = j

    strWidth = .Width - 2

    For k = 2 To j

        strWidth = strWidth & ";" & 0

    Next k

    .ColumnWidths = strWidth

    'Load list members into an array

    ReDim myArray(i, j)

    For n = 0 To j - 1

        For m = 0 To i - 1

            Set oRng = oTable.Cell(m + 2, n + 1).Range

            oRng.End = oRng.End - 1

            myArray(m, n) = oRng.Text

        Next m

    Next n

    'Populate the ListBox using the array

    .list() = myArray

    .ListIndex = 0

End With

'Close the source file

oSource.Close SaveChanges:=wdDoNotSaveChanges

End Sub

Private Sub CommandButton1_Click()

Set oVars = ActiveDocument.Variables

oVars("Address").Value = Me.ListBox1.Column(0) & vbCr & _

                         Me.ListBox1.Column(1) & vbCr & _

                         Me.ListBox1.Column(2) & vbCr & _

                         Me.ListBox1.Column(3)

ActiveDocument.Fields.Update

Unload Me

End Sub

Was this answer helpful?

0 comments No comments

42 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-04-10T02:09:01+00:00

    Another example of an unbound data source would be an Excel range:

    Private Sub UserForm_Initialize()

    Dim xlApp As Object

    Dim xlbook As Object

    Dim xlSheet As Object

    Dim Listarray As Variant

    Dim bStartApp As Boolean

    On Error Resume Next

    Set xlApp = GetObject(, "Excel.Application")

    If Err Then

      bStartApp = True

      Set xlApp = CreateObject("Excel.Application")

    End If

    On Error GoTo 0

    With xlApp

      Set xlbook = .Workbooks.Open("D:\Data Stores\ClientInfo.xlsx") 'change as appropriate

      Set xlSheet = xlbook.Sheets(1)

      Listarray = xlSheet.UsedRange.Value

      xlbook.Close SaveChanges:=False

      Set xlbook = Nothing

    End With

    If bStartApp Then xlApp.Quit

    Set xlApp = Nothing

    With ListBox1

      .ColumnCount = UBound(Listarray, 2)

      .Clear

      .List() = Listarray

      .RemoveItem (0) 'removes any header row

    End With

    Set xlApp = Nothing

    Set xlbook = Nothing

    Set xlSheet = Nothing

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-04-09T23:32:03+00:00

    Jumping in late in the thread, but I wanted to comment to clear something up.  The "10" column limit only applies when you are working with unbounded data sources.  If you have a bound data source then the limit is certainly more than 10.  I don't know what it really is but 35 is not a problem.

    You can run this code in a simple userform with two listboxes and a command button to demo:

    Option Explicit

    Private Sub CommandButton1_Click()

    On Error Resume Next

    MsgBox Me.ListBox1.Column(34)

    Me.Hide

    End Sub

    Private Sub UserForm_Initialize()

    'Create a bound data source

    Dim arrDemo(34, 1) As String

    Dim i As Long

    Dim j As Long

    'Populate the data source

    For i = 0 To 1

      For j = 0 To 34

        arrDemo(j, i) = "Row " & i + 1 & " Column " & j + 1

      Next j

    Next i

    'Set columns and populate

    Me.ListBox1.ColumnCount = UBound(arrDemo)

    Me.ListBox1.Column = arrDemo

    'Set columns (note no error)

    Me.ListBox2.ColumnCount = 34

    On Error GoTo Err_Handler

    For i = 0 To 1

      With Me.ListBox2

        .AddItem

        'Note error when j = 10

        For j = 0 To 34

          .Column(j, i) = "Test"

        Next j

      End With

    Next i

    Err_ReEntry:

    Exit Sub

    Err_Handler:

      MsgBox "It is only with unbounded data sources are you limited to " & j - 1 & " columns."

    Resume Err_ReEntry

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-04-09T06:48:20+00:00

    With all due respect to Hans, his is an answer to a different question to that addressed in this thread.

    If you want to populate 4 userform list boxes (which can have a maximum of 10 columns each) with the 35 columns from your table, display two columns in each and set the width of the form  (which is what you asked) then you would need something like

    Private Sub UserForm_Initialize()

    Dim xlApp As Object

    Dim xlWB As Object

    Dim xlWS As Object

    Dim cRows As Long

    Dim cCols As Long

    Dim i As Long, j As Long, k As Long, m As Long

    Dim strWidth As String

    Set xlApp = CreateObject("Excel.Application")

    Set xlWB = xlApp.Workbooks.Open("C:\Templates\TemplateDocs\ClientInfo.xlsx")

    Set xlWS = xlWB.Worksheets(1)

    On Error GoTo CleanUp

    cRows = xlWS.Range("A65536").End(xlUp).Row

    cCols = 10

    Me.Width = 280 'Set the width of the form

    With Me.ListBox1

        .ColumnCount = cCols

        strWidth = Int((.Width / 2) - 2)

        strWidth = strWidth & ";" & strWidth

        For k = 2 To cCols

            strWidth = strWidth & ";" & 0

        Next k

        .ColumnWidths = strWidth

        For i = 2 To cRows

            .AddItem xlWS.Cells(i, 1)

            For j = 1 To 9

                .list(.ListCount - 1, j) = xlWS.Cells(i, j + 1)

            Next j

        Next i

        .ListIndex = -1

    End With

    With Me.ListBox2

        .ColumnCount = cCols

        strWidth = .Width - 2

        .ColumnCount = cCols

        strWidth = Int((.Width / 2) - 2)

        strWidth = strWidth & ";" & strWidth

        For k = 2 To cCols

            strWidth = strWidth & ";" & 0

        Next k

        .ColumnWidths = strWidth

        For i = 2 To cRows

            .AddItem xlWS.Cells(i, 10)

            For j = 10 To 19

                .list(.ListCount - 1, j - 10) = xlWS.Cells(i, j + 1)

            Next j

        Next i

        .ListIndex = -1

    End With

    With Me.ListBox3

        .ColumnCount = cCols

        .ColumnCount = cCols

        strWidth = Int((.Width / 2) - 2)

        strWidth = strWidth & ";" & strWidth

        For k = 2 To cCols

            strWidth = strWidth & ";" & 0

        Next k

        .ColumnWidths = strWidth

        For i = 2 To cRows

            .AddItem xlWS.Cells(i, 20)

            For j = 20 To 29

                .list(.ListCount - 1, j - 20) = xlWS.Cells(i, j + 1)

            Next j

        Next i

        .ListIndex = -1

    End With

    With Me.ListBox4

        .ColumnCount = cCols

        .ColumnCount = cCols

        strWidth = Int((.Width / 2) - 2)

        strWidth = strWidth & ";" & strWidth

        For k = 2 To cCols

            strWidth = strWidth & ";" & 0

        Next k

        .ColumnWidths = strWidth

        For i = 2 To cRows

            .AddItem xlWS.Cells(i, 30)

            For j = 30 To 34

                .list(.ListCount - 1, j - 30) = xlWS.Cells(i, j + 1)

            Next j

        Next i

        .ListIndex = -1

    End With

    CleanUp:

    Set xlWS = Nothing

    Set xlWB = Nothing

    xlApp.Quit

    Set xlApp = Nothing

    End Sub

    'The following will set the value selected in the first box in the rest.

    Private Sub ListBox1_Change()

    Me.ListBox2.ListIndex = Me.ListBox1.ListIndex

    Me.ListBox3.ListIndex = Me.ListBox1.ListIndex

    Me.ListBox4.ListIndex = Me.ListBox1.ListIndex

    End Sub

    Was this answer helpful?

    0 comments No comments