Option Explicit
Private oVars As Variables
Private oSource1 As Document, oSource2 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 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)
oVars("Doctemplate").Value = Me.ListBox2.Column(1)
ActiveDocument.Fields.Update
Unload Me
End Sub
Private Sub UserForm_Initialize()
Set oSource1 = Documents.Open(FileName:="C:\Addresses.doc", _
AddToRecentFiles:=False, _
Visible:=False)
Set oSource2 = Documents.Open(FileName:="C:\Documents.doc", _
AddToRecentFiles:=False, _
Visible:=False)
Set oTable = oSource1.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
Set oTable = oSource2.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.ListBox2
.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
oSource1.Close SaveChanges:=wdDoNotSaveChanges
oSource2.Close SaveChanges:=wdDoNotSaveChanges
End Sub