Results 1 to 9 of 9

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

  1. #1

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

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

    This may seem rather trivial, but I would appreciate any help.

    I am currently building a Word 2003 template. When the template is opened (new), a userform is initialised. Choices made within the userform will build the completed document using bookmarks.

    Within the document I have a 1 row, 2 column table. I want to use the table to generate 2 lists of items - one in the left column and one in the right column (it is in fact one list, but I am separating into 2 columns for spacing). Each item in the list needs to be in its own cell. Before the user has made any choices I do not know how long the list will be, although I know there will be at least one item.

    In the first cell is a bookmark, from which my list begins. The first item is entered based on a user choice. The remaining items may or may not be entered so for each option that is selected I have created a string which is in the format:

    VB Code:
    1. strOption02 = vbTab & "Some text"

    If the option is not selected then the string is nothing.

    When the userform information is submitted, I then have:

    VB Code:
    1. .Bookmark("Table1Cell1").Range.Text = strFirstItem & strOption01 & strOption02.....& strOption50

    Any strings that are null are ignored so the table does not expand beyond the size it needs to be and the length of the "2" lists would be the same or worst case, the left column would have one entry more (or at least that was the plan).

    I was hoping that the vbTab would behave the same way as pressing the keyboard tab when in a table cell, i.e. tab to the next column or add a new row if already in the last column. As I'm sure many people know (except me of course), that doesn't work.

    Assuming my explanation is clear, can anyone offer any advice or direction with what I am trying to achieve?

    Thanks in advance.

    PS. I have tried recording a Macro tabbing from cells and adding text etc but I don't understand the code enough to apply it properly (constant syntax errors).
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

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

    Use the Tables collection to add text to a particular cell.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

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

    Quote Originally Posted by RobDog888
    Use the Tables collection to add text to a particular cell.
    Thanks for the response RobDog, but I can't see how that will work because the options within my userform not only add the text to the cell, but also determine whether the cell is there in the first place.

    Say for instance, I have 50 options, but only options 09, 23, and 32 are selected. Then, assuming what I am trying is feasible:

    My 1 row, 2 column tabke will become a 2 row, 2 column table.

    Cell(1,1) = strFirstItem (something is always inserted here)
    Cell(1,2) = text for option 09
    Cell(2,1) = text for option 23
    Cell(2,2) = text for option 32

    However, in another case the user may select options 03, 07, 15, 23, 27, 48 and 50, which would produce a 4 row, 2 column table.

    My problem is ignoring an option if nothing is selected and putting them in order without leaving blank spaces if something is selected.

    Hope this makes sense.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

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

    I dont quite understand but I think if you look at the table collection you will realise that there is a Row and Columns child collections. You can check what the dimensions of the table are dynamically. So it wont matter what options are choosen.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

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

    Thanks again RobDog, but maybe this one is a little beyond me for now. I have looked at Tables in vba help and google and can not find a way of achieving my goal (I have to plead ignorance at this point ).

    To Confirm:

    My table will always contain 2 columns, but the number of rows will vary in-line with the number of options selected.

    With each option selected, I want to add the option text either in column 2 of the last row or, if that cell already contains an option, then I want the option to add a row and then add text into the first column of the new row. After that the next selected option will move to the second column of the new row and add the option text. The cycle will then repeat for as many options as there are selected.

    So each option will do one of two things, depending on where the last entry was made, i.e. column 1 or column 2.

    Move from column 1 of the last row to column 2 of the last row, where it will add option text.

    OR

    Add a row and then add option text to the first column of that row.

    Perhaps spending more time on the tables collection will reveal a solution?

    Thanks again for your time.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

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

    Ok, I was just trying to get you to see the forest instead of the trees.

    The Tables' Rows collection has a .Count property. This will tell you how many rows exist in your table. You can reference any row by changing the index property of the Rows collection.

    VB Code:
    1. MsgBox "Rows in the first table: " & Tables(1).Rows.Count
    2.  
    3. Tables(1).Rows(Tables(1).Rows.Count).Cells(2).Range.Text = "Column 2"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

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

    Thanks.

    I've looked at the tables collection and am starting to understand it a little better. Still not quite sure exactly how to achieve my goal, but at least I have some direction.

    Not sure if it's possible or how to do it, but one route I was going to attempt was to somehow count the total number of selected options and then assign a resulting value to strings, which follow a numerical order. Based on the total number of options selected, I will generate a table with an equal or similar number of cells (I say similar because if the total number of options is odd, then the last cell, last column will be empty). I then need to assign the strings to cells in order.

    For example:

    If the user selects option04, option08, option12 and option15, then:

    str00 = Compulsory option text
    str01 = option04 text
    str02 = option08 text
    str03 = option12 text
    str04 = option15 text

    Now I need to generate a table based on the number of valid strings, i.e. 5, meaning that I need a 3x2 table (6 cells) and:

    cell(1,1) = str00
    cell(1,2) = str01
    cell(2,1) = str02
    cell(2,2) = str03
    cell(3,1) = str04
    cell(3,2) = null

    Again, not sure how to do this, or if it's feasible, but with my limited understanding of vba, it's the only approach I can think of even trying!

    And just to clarify my way of thinking, if the user selected option06, option07 and option23 then:

    str00 = Compulsory option text
    str01 = option06 text
    str02 = option07 text
    str03 = option23 text

    In this instance I would need to generate a 2x2 table and:

    cell(1,1) = str00
    cell(1,2) = str01
    cell(2,1) = str02
    cell(2,2) = str03

    Again, thanks for your time and patience.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  8. #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

  9. #9

    Thread Starter
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

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

    Thanks DKenny, that works perfectly (as you would expect).

    I must admit there are many aspects of the code I have never seen before and don't fully understand, but I'm sure I'll have no problems working it out from the sample you kindly provided.

    Once again, my sanity has been preserved!

    EDIT

    Seems I am having problems after all DKenny! I clearly overestimated my abilities!

    The problem I have is that my table already exists. What I want to do is add rows and add the option text tot those rows (the pre-existing cells will be poulated using bookmarks because they will always contain something). Attached is a revised version of the sample you kindly provided, which no longer works.

    It adds the correct number of rows, but because I have removed tblMytable, it no longer fills them with the option text. I know why it doesn't work, and have made numerous attempts at fixing it, but have hit a brick wall.

    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, 9) = "chkOption" Then
    16.             '...that are checked....
    17.             If chkMyCheck.Value = True 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 of rows and 2 columns
    37.    
    38. 'CHANGED FROM:
    39.     'Set myRange = ActiveDocument.Range(Start:=0, End:=0)
    40.     'Set tblMyTable = ActiveDocument.Tables.Add(Range:=myRange, NumRows:=lRowCount, NumColumns:=2)
    41. 'TO
    42.     ActiveDocument.Tables(1).Select
    43.     Selection.InsertRowsBelow (lRowCount)
    44.    
    45.     'loop through the array - adding the values to the table
    46.     For lItem = LBound(asSelected) To UBound(asSelected) 'lItem = SIZE OF ARRAY
    47.         'First we fill the first column
    48.         If lItem <= lRowCount Then ''lItem LESS THAN OR EQUAL TO NUMBER OF ROWS
    49.             [COLOR=RoyalBlue]tblMyTable.Cell(lItem, 1).Range.InsertAfter asSelected(lItem)[/COLOR]
    50.        
    51.         'then we fill the second column
    52.         Else
    53.            [COLOR=RoyalBlue]tblMyTable.Cell(lItem - lRowCount, 2).Range.InsertAfter asSelected(lItem)[/COLOR]        
    54.     End If
    55.     Next lItem
    56.    
    57.     'then close the form
    58.     Unload Me
    59. End Sub

    Most of the code you provided is totally new to me, but I have broke it down and believe I understand the overall process.

    However, I can't figure out the lines I've highlighted or the looping process in general. I've added commments to the parts I think I understand.

    Any help or advice that you can offer would be appreciated.

    Thanks

    ANOTHER EDIT

    DKenny, problem solved (I think!)

    I know there are always going to be 3 rows in my document so I simply changed the code for adding option text to:

    VB Code:
    1. 'loop through the array - adding the values to the table
    2.     For lItem = LBound(asSelected) To UBound(asSelected)
    3.         'First we fill the first column
    4.         If lItem <= lRowCount Then
    5.             ActiveDocument.Tables(1).Cell(lItem + 3, 1).Range.InsertAfter asSelected(lItem)
    6.        
    7.         'then we fill the second column
    8.         Else
    9.            ActiveDocument.Tables(1).Cell(lItem - lRowCount + 3, 2).Range.InsertAfter asSelected(lItem)
    10.         End If
    11.     Next lItem

    I suppose the bit that was putting me off is that I have never used FOR.. NEXT and these things can sometimes seem more complicated than they are, although I still don't fully understand the statements that actually add the text and would still appreciate any feedback that you can offer.

    Thanks again.

    EDIT

    The code had to be changed again to allow for no options being selected, which produced an error because lRowCount = 0. To resolve this I added:

    VB Code:
    1. If lRowCount > 0 Then
    2.     ActiveDocument.Tables(1).Select
    3.     Selection.InsertRowsBelow (lRowCount)
    4.     End If
    Attached Files Attached Files
    Last edited by New2vba; Feb 27th, 2006 at 04:41 AM. Reason: NEW PROBLEM
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

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