[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:
strOption02 = vbTab & "Some text"
If the option is not selected then the string is nothing.
When the userform information is submitted, I then have:
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 )
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 )
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.
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 )
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:
MsgBox "Rows in the first table: " & Tables(1).Rows.Count
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:
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.
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.
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:
If lRowCount > 0 Then
ActiveDocument.Tables(1).Select
Selection.InsertRowsBelow (lRowCount)
End If
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 )