Interesting mix of bookmarks and variables, but the following should work. You can use
http://www.gmayor.com/BookmarkandVariableEditor.htm to see exactly what is inserted into the variables and bookmarks.
It is not clear what ListBox 2 is doing and you appear to be using a mix of document and excel data sources rather than one or the other, for no obvious reason, but it should work.
Option Explicit
Private oVars As Variables
Private oSource1 As Document
Private oTable As Table
Private oRng As Range
Private strWidth As String
Private sVar As String
Private i As Long, j As Long, k As Long
Private m As Long, n As Long
Private Sub CmdBtnCancel_Click()
Unload Me
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)
For i = 0 To Me.ListBox2.ColumnCount - 1
sVar = str(i + 1)
oVars(sVar).Value = Me.ListBox2.Column(i)
Next i
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()
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim cRows As Long
Dim cCols As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim strWidth As String
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open("C:\Templates\TemplateDocs\ClientIntake.xlsx")
Set xlWS = xlWB.Worksheets(1)
Set oSource1 = Documents.Open(FileName:="C:\Templates\TemplateDocs\Addresses.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
oSource1.Close SaveChanges:=wdDoNotSaveChanges
ListBox2.ColumnCount = cCols
With Me.ListBox2
cRows = xlWS.Range("A1").CurrentRegion.Rows.Count
cCols = xlWS.Range("A1").CurrentRegion.Columns.Count
.ColumnCount = cCols
strWidth = .Width - 2
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 cCols
.list(.ListCount - 1, j) = xlWS.Cells(i, j + 1)
Next j
Next i
End With
Set xlWS = Nothing
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
With Me.ListBox3
.ColumnCount = 4
.ColumnWidths = "150;0;0;0"
.AddItem "NAME1"
.list(0, 1) = "Yours truly,"
.list(0, 2) = "bbNAME1"
.list(0, 3) = "Job Title"
.AddItem "NAME2"
.list(1, 1) = "Yours truly,"
.list(1, 2) = "bbNAME2"
.list(1, 3) = "Job Title"
.AddItem "NAME3"
.list(2, 1) = "Yours truly,"
.list(2, 2) = "bbNAME3"
.list(2, 3) = "Job Title"
.AddItem "NAME4"
.list(3, 1) = "Yours truly,"
.list(3, 2) = "bbNAME4"
.list(3, 3) = "Job Title"
.AddItem "NAME5"
.list(4, 1) = "Yours truly,"
.list(4, 2) = "bbNAME5"
.list(4, 3) = "Job Title"
End With
End Sub