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!

table row height problems!?

Status
Not open for further replies.

corman

Technical User
Jan 31, 2003
4
CA
Hello. Here's my problem. I'm using Word 97 by the way.

I have a 7-column table, with just one row (containing the column headings). I use vba to add a new row to this table, and textboxes on the userform to populate the cells of the table. I also use code to limit the total height of the table (by counting the height of each row).

My userform has three command buttons: one to add the new row/inforamtion and then continue, one to add the row/info and stop, and one to cancel/unload the form.

My problem is with how Word is treating the height of the table rows.

When i add new data, no matter how much data I enter into the textbox, the new row is added properly with the row height set to fit all the data as needed (height increases if the text needs to wrap in the table cell). Now, for some reason, as I add more rows, the height of the third row up (not the new added row, or the row above the new one, but the one aboe THAT one) changes! it becomes quite small, thus hiding some of the info in the cells (especially if the text has had to wrap). Even the row with the column headings is changed.

For instance (pretend the table rows are numbered, starting from 1):

If you have added 10 rows of data (for a total of 11 rows), all but the last two rows (rows 10 and 11) are having their row heights changed by my code (I don't want this to happen!). If you then add another row (total of 12), it changes the row height of row 10 to a smaller height so that only rows 11 and 12 are as they should be. The best way to visualize it is to make a empty 7-column,1-row table and try the code I guess.

Basically, I want each row to automatically resize to fit the amount of text entered through the userform. Some rows may be small, some may be large.

Also, is there any other way I can limit the size of the table besides adding the individual row heights? (I think my problem may be rooted in the height rule portions of the code - in order to get the height, I need to set the height rule to exactly instead of auto)

Any help/insight is appreciated!

Here’s my code:

-----------

Option Explicit


Private Sub AddButton_Click()

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

' 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

myTable1.Rows(RowCount1).HeightRule = wdRowHeightAuto

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

' add new row

Set NewRow1 = myTable1.Rows.Add
LastRow1 = myTable1.Rows.Count
myTable1.Rows(LastRow1).HeightRule = wdRowHeightAtLeast
' using Auto in the above line of code doesn't work either!


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

' reset variable counters
RowCount1 = 0
TotalHeight1 = 0
AddRowCheck1 = 0
LastRow1 = 0

' reset values in textbox on form to null
txtTestNo.Value = &quot;&quot;
txtLocation.Text = &quot;&quot;
txtDryWt.Value = &quot;&quot;
txtMC.Value = &quot;&quot;
txtProctor.Value = &quot;&quot;
txtOptMC.Value = &quot;&quot;
txtDensity.Value = &quot;&quot;

End Sub



Private Sub CancelButton_Click()
Unload frmDensities
End Sub



Private Sub DoneButton_Click()

' same code a above, but unloads userform after adding info
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

' 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

Set NewRow1 = myTable1.Rows.Add
LastRow1 = myTable1.Rows.Count
myTable1.Rows(LastRow1).HeightRule = wdRowHeightAtLeast
' using Auto doesn't work either!


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

Unload frmDensities

End Sub

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

Thanks in advance for any help!

Corey Dale
 
Setting heightrule=wdRowHeightAuto seems to work for me. Your code comments suggests it doesn't for you - what happens that you're not happy with?
p.s. starting a new thread for an identical problem tends to confuse issues. please try to follow up in the original thread when you post about a problem.
Rob
[flowerface]
 
Sorry about that. I guess I should have posted it in the same thread (it was essentially the same question, even though the code was different).

Setting the height rule to auto didn't to anything for me, like my comment states in the code. The code performed the same way with it at exactly, auto, or not there at all.

To probelm solve, I tried the following: I set the initial row height conditions of the table to 'auto' through the table menu ('table...', 'cell height and width...', etc.) There was something weird going on in there, because as I added more rows, it code was changing it from auto to exactly (checked it through 'table...', 'cell height and width...', etc.). Strange. But is always changed the third row up from the bottom (after the new row was added).

I did finally get in to work properly by using a work-around. I added three lines of code to run through a loop that reset the heightrule of each row back to 'auto' right after the new row is added and counted:

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

' add new row
Set NewRow1 = myTable1.Rows.Add
LastRow1 = myTable1.Rows.Count
myTable1.Rows(LastRow1).HeightRule = wdRowHeightAuto
<color red>For d = 1 To LastRow1
myTable1.Rows(d).HeightRule = wdRowHeightAuto
Next d</color>

' Fill in the table row
myTable1.Cell(LastRow1, 1).Range.Text = txtTestNo.Value
myTable1.Cell(LastRow1, 2).Range.Text = txtLocation.Text
myTable1.Cell(LastRow1, 3).Range.Text = txtDryWt.Value
myTable1.Cell(LastRow1, 4).Range.Text = txtMC.Value
myTable1.Cell(LastRow1, 5).Range.Text = txtProctor.Value
myTable1.Cell(LastRow1, 6).Range.Text = txtOptMC.Value
myTable1.Cell(LastRow1, 7).Range.Text = txtDensity.Value
Else
' dont add row and give msgbox instead
MsgBox &quot;There are too many rows! Start a new file!&quot;
<color red>LastRow1 = myTable1.Rows.Count
For d = 1 To LastRow1
myTable1.Rows(d).HeightRule = wdRowHeightAuto
Next d</color>
End If
.
.
.
.

I do appreciate the help. I just wish I knew why the heck it seemed to work the original way for everyone but me! :)

Corey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top