Here is a little procedure that I have been working on for some time. I believe its purpose is clear in the comments at the beginning of the procedure. However, it will take information from a listbox and insert that information into a table of the same number of columns.
As always, I would be interested in tightening this up. So your comments are solicited.
Oh, the code has been formatted at 80 characters, so there should not be any "wrap" problems.
<pre>Public Sub mInsertPriorityRows( _
ByVal vctlListBox As Control, _
ByVal vwdTransferTo As Word.Document, _
ByVal vstrTableBookmark As String)
'-------------------------------------------------------
'General: This procedure takes the information from a list box in a userform
' and inserts it into a table of the same number of columns. The
' number of columns is defined by (tabReceivingTable.Columns.Count - 1)
'
'Specifically: The procedure sets tabReceivingTable variable to Table(1)
' of the range defined by vstrTableBookmark. This is a bookmark that
' surrounds a two-row table, row 1 being the heading row.
' It then copies row 2 and starts inserting records from vctlListBox.
' Each "cell" of vctlListBox is inserted into the corresponding cell
' in tabReceivingTable, starting with row 2, in reverse order, i.e.,
' last in, first out. Row 2, at this point, is the last row of the
' table. If it is not the last row to be inserted, a new row is inserted _
' before row 2, thus becoming a new row 2. And the procedure loops.
'
'Suppositions:
' 1. A two column table is in vwdTransferTo
' 2. A bookmark with name vstrTableBookmark surrounds the table
' 3. vctlListBox does exist
'-------------------------------------------------------
Dim rngReceivingTable As Range
Dim tabReceivingTable As Table
Dim intCounterRow As Integer
Dim intCounterColumn As Integer
Set rngReceivingTable = vwdTransferTo.Bookmarks(vstrTableBookmark).Range
Set tabReceivingTable = rngReceivingTable.Tables(1)
tabReceivingTable.Rows.Last.Range.Copy
For intCounterRow = vctlListBox.ListCount To 1 Step -1
For intCounterColumn = 0 To (tabReceivingTable.Columns.Count - 1)
tabReceivingTable.Rows(2). _
Cells(intCounterColumn + 1).Range.Text = _
vctlListBox.List(intCounterRow - 1, intCounterColumn)
Next intCounterColumn
If intCounterRow > 1 Then
tabReceivingTable.Rows.Add tabReceivingTable.Rows(2)
End If ' intCounter > 1
Next intCounterRow
End Sub</pre>



