I'm sorry, I can't seem to incorporate what's on your page with my current code. I'm trying to use this one:
Private Sub Userform_Initialize()
'Late binding. No reference to Excel Object required.
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim cRows As Long
Dim i As Long
Set xlApp = CreateObject("Excel.Application")
'Open the spreadsheet to get data
Set xlWB = xlApp.Workbooks.Open("D:\Data Stores\sourceSpreadsheet.xls")
Set xlWS = xlWB.Worksheets(1)
cRows = xlWS.Range("mySSRange").Rows.Count - xlWS.Range("mySSRange").Row + 1
ListBox1.ColumnCount = 3
'Populate the listbox.
With Me.ListBox1
For i = 2 To cRows
'Use .AddItem property to add a new row for each record and populate column 0
.AddItem xlWS.Range("mySSRange").Cells(i, 1)
'Use .List method to populate the remaining columns
.List(.ListCount - 1, 1) = xlWS.Range("mySSRange").Cells(i, 2)
.List(.ListCount - 1, 2) = xlWS.Range("mySSRange").Cells(i, 3)
Next i
End With
'Clean up
Set xlWS = Nothing
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
lbl_Exit:
Exit Sub
End Sub
Below is my current code (Not sure if you would need to see the entire thing or just the initialize event so I copied everything below). I would like to change oSource2 to use your example above.
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 CmdBtnCancel_Click()
Me.hide
End Sub
Private Sub CommandButton1_Click()
With ActiveDocument
.Variables("Salutation").Value = Me.TextBox1.Text
.Variables("Attention").Value = Me.TextBox2.Text
.Variables("SentBy").Value = Me.TextBox3.Text
.Variables("Period").Value = Me.TextBox4.Text
.Variables("FileUnder").Value = Me.TextBox5.Text
.Variables("Amount").Value = Me.TextBox6.Text
.Fields.Update
End With
Set oVars = ActiveDocument.Variables
oVars("Address").Value = Me.ListBox1.Column(1) & vbCr & _
Me.ListBox1.Column(2) & vbCr & _
Me.ListBox1.Column(3) & vbCr & _
Me.ListBox1.Column(4)
oVars("CFName").Value = Me.ListBox2.Column(0)
oVars("CLName").Value = Me.ListBox2.Column(1)
oVars("1").Value = Me.ListBox2.Column(2)
oVars("2").Value = Me.ListBox2.Column(3)
oVars("3").Value = Me.ListBox2.Column(4)
oVars("4").Value = Me.ListBox2.Column(5)
oVars("5").Value = Me.ListBox2.Column(6)
oVars("6").Value = Me.ListBox2.Column(7)
oVars("7").Value = Me.ListBox2.Column(8)
oVars("8").Value = Me.ListBox2.Column(9)
oVars("9").Value = Me.ListBox2.Column(10)
oVars("10").Value = Me.ListBox2.Column(11)
oVars("11").Value = Me.ListBox2.Column(12)
oVars("12").Value = Me.ListBox2.Column(13)
oVars("13").Value = Me.ListBox2.Column(14)
oVars("14").Value = Me.ListBox2.Column(15)
oVars("15").Value = Me.ListBox2.Column(16)
oVars("16").Value = Me.ListBox2.Column(17)
oVars("17").Value = Me.ListBox2.Column(18)
oVars("18").Value = Me.ListBox2.Column(19)
WriteTextToBM "Closing", Me.ListBox3.Column(1)
WriteTextToBM "Name", Me.ListBox3.Column(0)
WriteTextToBM "Position", Me.ListBox3.Column(3)
InsertBBInBM "SigImage", Me.ListBox3.Column(2)
ActiveDocument.Fields.Update
Unload Me
End Sub
**Private Sub Userform_Initialize()**Set oSource1 = Documents.Open(FileName:="C:\Templates\TemplateDocs\Addresses.doc", _
AddToRecentFiles:=False, _
Visible:=False)
Set oSource2 = Documents.Open(FileName:="C:\Templates\TemplateDocs\ClientInfo.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 'The number of columns in the table
strWidth = (.Width / 2) - 2 & ";" & (.Width / 2) - 2 'Set the first two column widths to suit the available space - you can use actual measurements if you prefer then to calculated measurements
For k = 3 To j 'set columns 3 to the total to 0 width
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
'Close the source file
oSource1.Close SaveChanges:=wdDoNotSaveChanges
oSource2.Close SaveChanges:=wdDoNotSaveChanges
End With
Thank you!