Populate a combo box  using a CSV in Word

PLA_LTM
PLA_LTM used Ask the Experts™
on
Hello all

I have a Mail Merge main document which is looking at a CSV file as the source file.  This CSV file may have hundreds of records.  There is a column in this CSV file that contains US States.  At any given time a user will only need to merge info for one or 2 States at a time.  The way the CSV file is produced there may be many records and all the needed information is scattered through out the source file.  

Currently the user clicks on the Mail Merge Recipients button, sorts by States, and selects only the records needed.  This worked well at one time but now it's proving to be very time consuming.

I am looking for a way to provide the user with a Drop Down list that is "linked" to this States column.  So that each time the Main Document is open it will refresh and only the available states will be presented to the user.

Once selected and when the user clicks merge, only the records matching the selected criteria should be merged.

Any help would be appreciated.

Thank you in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Top Expert 2012
Commented:
You will need to put some code in the merge Main document and also to add a UserForm. Put a combobox and a command button on the userform
In the ThisDocument module:
 
Private Sub Document_Open()
    UserForm1.Show vbModal
End Sub
 
In the UserForm:
 
Option Explicit
 
Private Sub CommandButton1_Click()
    Dim i As Integer
    With ThisDocument.MailMerge.DataSource
        .SetAllIncludedFlags True
        For i = i To .RecordCount
            .ActiveRecord = i
            .Included = (.DataFields("State").Value = ComboBox1.Text)
        Next i
    End With
    Unload Me
End Sub
 
Private Sub UserForm_Activate()
    Dim i As Integer
    Dim j As Integer
    Dim bAdded As Boolean
    
    ComboBox1.Clear
    With ThisDocument.MailMerge.DataSource
        .SetAllIncludedFlags True
        For i = i To .RecordCount
            .ActiveRecord = i
            bAdded = False
            For j = 0 To ComboBox1.ListCount - 1
                If .DataFields("State").Value = ComboBox1.List(j) Then
                    bAdded = True
                    Exit For
                End If
                If .DataFields("State").Value < ComboBox1.List(j) Then
                    ComboBox1.AddItem .DataFields("State").Value, j
                    bAdded = True
                End If
            Next j
            If Not bAdded Then
                ComboBox1.AddItem .DataFields("State").Value
            End If
        Next i
    End With
 
End Sub

Open in new window

Author

Commented:
This is Great, exactly what I'm looking for!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial