Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem addings rows to a table in Word

Status
Not open for further replies.

corman

Technical User
Jan 31, 2003
4
CA
Hello.

I have a table with 7 columns, which starts off with one row (with row headings). I want to user to enter information into a textbox on a userform, and have the data placed into a new row in the table.

My problem is that when word adds a new row to a table, it gives the new row the same default size as the last row of the table. I want the height of the new row to be dependant upon the amount of data inputted into the userform textbox.

As it stands, if I have a lot of data to enter into the table, the table row height automatically increases to fit the text (good), but if the NEXT row has a much SMALLER amount of data (enough so as to not wrap to a new line with the cell), the new line still gets the same large row height as the last one (instead of being just large enough to fit the text).

I want to be able to add a new row, with (for instance) a very small row height, then insert the data into the row and have the row automatically resize to fit the text. I won't this to happen for each new row added. I'm having some trouble with the coding to make this happen!

(further, so you understand the rest of the code, I want to limit to total verical size of the table. The code currently uses the assumption that the new added row will be the same height as the last row when checking the total table height. Advice on ways to get around this would be appreciated too!)


Here is my code:

---------------

Dim TotalHeight As Single
Dim myTable As Table
Dim RowCount As Integer
Dim LastRow As Integer

' Reference the first table in the document.
Set myTable = ActiveDocument.Tables(1)

RowCount = myTable.Rows.Count

' get total height of table
For c = 1 To RowCount
myTable.Rows(c).HeightRule = wdRowHeightExactly
TotalHeight = TotalHeight + myTable.Rows(c).Height
Next c

' get height of last row
AddRowCheck = TotalHeight + myTable.Rows(RowCount).Height

' add height of last row to current total height to see if
' adding a new row will make the table too large

If AddRowCheck < 135 Then ' 1.87&quot; * 72 (convert to points)

' add new row

Set newRow = myTable.Rows.Add
LastRow = myTable.Rows.Count

' Fill in the table row. this data will eventually
' come from a userform

myTable.Cell(LastRow, 1).Range.Text = &quot;Cell 1 Text&quot;
myTable.Cell(LastRow, 2).Range.Text = &quot;Cell 2 Text&quot;
myTable.Cell(LastRow, 3).Range.Text = &quot;Cell 3 Text Cell 3 Text Cell 3 _
Text Cell 3 Text Cell 3 Text Cell 3 Text&quot;
myTable.Cell(LastRow, 4).Range.Text = &quot;Cell 4 Text&quot;
myTable.Cell(LastRow, 5).Range.Text = &quot;Cell 5 Text&quot;
myTable.Cell(LastRow, 6).Range.Text = &quot;Cell 6 Text&quot;
myTable.Cell(LastRow, 7).Range.Text = &quot;Cell 7 Text&quot;
Else
' dont add row and give msgbox instead
MsgBox &quot;There are too many rows to add another&quot;
End If

----------------------------

Any help is appreciated! :)

Thanks,
Corey
 
When trying manually, I can't reproduce the behavior you describe - Word always adds a new row of the minimum size (one line). Could it be something about the settings for your table?
Rob
[flowerface]
 
Thanks for the reply. I'm not sure if I described my problem well enough.

I've made some changes to help describe it better (and so it will hopefully reproduce like it is for me). I'm using two command buttons on the word document to simulate what would happen through the userform. The first button assumes that the data entered into the textbox on the form does not make the text in the table cell wrap to another line, while the second button enters data that makes the row wrap.

I initially have a 7-column table, with just one row (this row containing the column headings). The new row is added and the 'virtual' textbox data is entered into the appropriate cell.

Here's is what happens for me:

When I press the second command button, a new row is added. The row height of this row increases due to the large amount of text entered into the cell in column 3. If I press it again, a new row is entered, again with a large row height. BUT, the row height of the previous row I added changes to be much smaller! This happens each time I add a row, until I have numerous thin rows (with some of the data hidden) and the last large row).

When I press the first command button from the start (i.e. only the header row is there, no presses of the second button at all), the rows are added nicely, and all stay at the same row height (i.e. they do not go smaller).

When I combine both buttons, the second button action rules. For example, if I press the first button first I get a nice new row. If I press it again – another nice row. If I then press the second button (for extra line, wrapping text), it makes the both the above added rows smaller, and the new one large enough handle all the new text. If I press it again, the large row becomes as small as the others, and the new row stays large.


Here’s my new code:


Private Sub CommandButton1_Click() ‘ only add a single line of text into table cells

Dim TotalHeight1 As Single
Dim myTable1 As Table
Dim NewRow1 As Variant
Dim RowCount1 As Integer
Dim LastRow1 As Variant, c As Integer, AddRowCheck1 As Variant

' Reference the first table in the document.
Set myTable1 = ActiveDocument.Tables(1)

RowCount1 = myTable1.Rows.Count

myTable1.Rows(RowCount1).HeightRule = wdRowHeightExactly

' get total height of table
For c = 1 To RowCount1
'myTable1.Rows(c).HeightRule = wdRowHeightExactly
TotalHeight1 = TotalHeight1 + myTable1.Rows(c).Height
Next c

' get height of last row
AddRowCheck1 = TotalHeight1 + myTable1.Rows(RowCount1).Height

If AddRowCheck1 < 135 Then ' 1.87 inches * 72 to convert to points

' add new row

'myTable.Rows.HeightRule = wdRowHeightAuto
'myTable1.Rows(LastRow1).HeightRule = wdRowHeightAuto
Set NewRow1 = myTable1.Rows.Add
LastRow1 = myTable1.Rows.Count
'myTable1.Rows(LastRow1).Height = 15
myTable1.Rows(LastRow1).HeightRule = wdRowHeightAtLeast 'auto doesn't work either!


' Fill in the table row
myTable1.Cell(LastRow1, 1).Range.Text = &quot;Cell 1 Text&quot;
myTable1.Cell(LastRow1, 2).Range.Text = &quot;Cell 2 Text&quot;
myTable1.Cell(LastRow1, 3).Range.Text = &quot;Cell 3 Text&quot;
myTable1.Cell(LastRow1, 4).Range.Text = &quot;Cell 4 Text&quot;
myTable1.Cell(LastRow1, 5).Range.Text = &quot;Cell 5 Text&quot;
myTable1.Cell(LastRow1, 6).Range.Text = &quot;Cell 6 Text&quot;
myTable1.Cell(LastRow1, 7).Range.Text = &quot;Cell 7 Text&quot;
Else
' dont add row and give msgbox instead
MsgBox &quot;There are too many rows! Start a new file!&quot;
End If

End Sub



Private Sub CommandButton2_Click() ‘ add text that wraps and increases the row height in one column

Dim TotalHeight As Single
Dim myTable As Table
Dim NewRow As Variant
Dim RowCount As Integer
Dim LastRow As Integer, c As Integer, AddRowCheck As Integer

' Reference the first table in the document.
Set myTable = ActiveDocument.Tables(1)

RowCount = myTable.Rows.Count

myTable.Rows(RowCount).HeightRule = wdRowHeightExactly

' get total height of table
For c = 1 To RowCount
TotalHeight = TotalHeight + myTable.Rows(c).Height
Next c

' get height of last row
AddRowCheck = TotalHeight + myTable.Rows(RowCount).Height

If AddRowCheck < 135 Then ' 1.87 inches * 72 to convert to points

' add new row

'myTable.Rows(LastRow).HeightRule = wdRowHeightAuto
Set NewRow = myTable.Rows.Add
LastRow = myTable.Rows.Count
'myTable.Rows(LastRow).Height = 15
myTable.Rows(LastRow).HeightRule = wdRowHeightAtLeast 'auto doesn't work either!

' Fill in the table row
myTable.Cell(LastRow, 1).Range.Text = &quot;Cell 1 Text&quot;
myTable.Cell(LastRow, 2).Range.Text = &quot;Cell 2 Text&quot;
myTable.Cell(LastRow, 3).Range.Text = &quot;Cell 3 Text Cell 3 Text Cell 3 Text Cell 3 Text&quot;
myTable.Cell(LastRow, 4).Range.Text = &quot;Cell 4 Text&quot;
myTable.Cell(LastRow, 5).Range.Text = &quot;Cell 5 Text&quot;
myTable.Cell(LastRow, 6).Range.Text = &quot;Cell 6 Text&quot;
myTable.Cell(LastRow, 7).Range.Text = &quot;Cell 7 Text&quot;
Else
' dont add row and give msgbox instead
MsgBox &quot;There are too many rows! Start a new file!&quot;
End If

End Sub

 
I haven't tried to run your code. However, I think I may see the problem. Near the top of your code, you set the heightrule for the last row to &quot;exactly&quot;. After you add the new row, you set ITS heightrule to &quot;at least&quot;. But you don't appear to reset the heightrule for the previous row (that one is commented out). Why do you need to set the rule to &quot;exactly&quot; in the first place? If you do need to do that, maybe first query the actual height, then set the rule to exactly AND the height to the actual height
something like:
h=myTable1.Rows(RowCount1).Height
myTable1.Rows(RowCount1).HeightRule = wdRowHeightExactly
myTable1.Rows(RowCount1).Height=h
Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top