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" * 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 = "Cell 1 Text"
myTable.Cell(LastRow, 2).Range.Text = "Cell 2 Text"
myTable.Cell(LastRow, 3).Range.Text = "Cell 3 Text Cell 3 Text Cell 3 _
Text Cell 3 Text Cell 3 Text Cell 3 Text"
myTable.Cell(LastRow, 4).Range.Text = "Cell 4 Text"
myTable.Cell(LastRow, 5).Range.Text = "Cell 5 Text"
myTable.Cell(LastRow, 6).Range.Text = "Cell 6 Text"
myTable.Cell(LastRow, 7).Range.Text = "Cell 7 Text"
Else
' dont add row and give msgbox instead
MsgBox "There are too many rows to add another"
End If
----------------------------
Any help is appreciated!![Smile :) :)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Thanks,
Corey
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" * 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 = "Cell 1 Text"
myTable.Cell(LastRow, 2).Range.Text = "Cell 2 Text"
myTable.Cell(LastRow, 3).Range.Text = "Cell 3 Text Cell 3 Text Cell 3 _
Text Cell 3 Text Cell 3 Text Cell 3 Text"
myTable.Cell(LastRow, 4).Range.Text = "Cell 4 Text"
myTable.Cell(LastRow, 5).Range.Text = "Cell 5 Text"
myTable.Cell(LastRow, 6).Range.Text = "Cell 6 Text"
myTable.Cell(LastRow, 7).Range.Text = "Cell 7 Text"
Else
' dont add row and give msgbox instead
MsgBox "There are too many rows to add another"
End If
----------------------------
Any help is appreciated!
Thanks,
Corey