Results 1 to 3 of 3
  1. #1

    ListBox to Table procedure (Word97 SR2)

    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.
    ' 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)


    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>

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Queanbeyan, New South Wales, Australia
    Thanked 0 Times in 0 Posts

    Re: ListBox to Table procedure (Word97 SR2)


    two small suggestions:

    .You don't need to define rngReceivingTable:
    Set tabReceivingTable = ActiveDocument.Bookmarks(vstrTableBookmark).Range. Tables(1)

    .Do you need to copy the last row?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3

    Re: ListBox to Table procedure (Word97 SR2)

    When I first saw the line "Do you need to copy the last row, "my thought was, "Of course I do!"

    Then as I started looking some more at the procedure, I saw I was using rows.add instead of paste. When I first started working on this, I *was* going to use paste, therefore, copying the last row was necessary. However, as you can see, it didn't work out that way.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts