Results 1 to 9 of 9

Thread: [RESOLVED] How to add text to table cells in Word 2003

Threaded View

  1. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How to add text to table cells in Word 2003

    here's an example of one way to build the table on the fly. I've setup an array where I store the captions of all checked boxes on a form.
    Then based on the number selected I build a table with the correct number of rows.
    Then I loop through the array, passing the value of each caption to the table.

    VB Code:
    1. Private Sub cmdBuildTable_Click()
    2. Dim asSelected() As String
    3. Dim chkMyCheck As Control
    4. Dim lRowCount As Long
    5. Dim myRange As Range
    6. Dim tblMyTable As Table
    7. Dim lItem As Long
    8.    
    9.     'Create an array to store the selected options
    10.     ReDim asSelected(1) As String
    11.    
    12.     'loop through the controls..
    13.     For Each chkMyCheck In Me.Controls
    14.         '..looking for checkboxes...
    15.         If Left(chkMyCheck.Name, 3) = "chk" Then
    16.             '...that are checked....
    17.             If chkMyCheck.Value Then
    18.                 '....and adding them to the array
    19.                 asSelected(UBound(asSelected)) = chkMyCheck.Caption
    20.                 'Then increasing the size of the array by 1
    21.                 ReDim Preserve asSelected(UBound(asSelected) + 1) As String
    22.             End If
    23.         End If
    24.     Next chkMyCheck
    25.    
    26.     'Make sure the array has an even number of elements by
    27.     'removing the extra record if there are an odd number
    28.     'of elements
    29.     If UBound(asSelected) Mod 2 = 1 Then
    30.         ReDim Preserve asSelected(UBound(asSelected) - 1) As String
    31.     End If
    32.    
    33.     'Determine how many rows to put in the table
    34.     lRowCount = UBound(asSelected) / 2
    35.    
    36.     'Add a new table, with the correct number o frows and 2 columns
    37.     Set myRange = ActiveDocument.Range(Start:=0, End:=0)
    38.     Set tblMyTable = ActiveDocument.Tables.Add(Range:=myRange, NumRows:=lRowCount, NumColumns:=2)
    39.    
    40.     'loop through the array - adding the values to the table
    41.     For lItem = LBound(asSelected) To UBound(asSelected)
    42.         'First we fill the first column
    43.         If lItem <= lRowCount Then
    44.             tblMyTable.Cell(lItem, 1).Range.InsertAfter asSelected(lItem)
    45.        
    46.         'then we fill the second column
    47.         Else
    48.             tblMyTable.Cell(lItem - lRowCount, 2).Range.InsertAfter asSelected(lItem)
    49.         End If
    50.     Next lItem
    51.    
    52.     'then close the form
    53.     Unload Me
    54. End Sub
    Attached Files Attached Files
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width