adding a table row, without setting same defaults as previous row 
Author Message
 adding a table row, without setting same defaults as previous row

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, but if the
NEXT row has a much samller 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.

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.  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 in checking the total table height.  Advice on ways to get around
this would be helpful 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 inches * 72 to 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

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

Please post a repsonse here, and by email to

Thanks,
Corey



Wed, 20 Jul 2005 05:43:06 GMT  
 adding a table row, without setting same defaults as previous row
Hi Corey,

Maybe the following does the trick for you??

Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)

With oTbl
    .Rows.Add
    With .Rows(.Rows.Count)
        .Height = 12
        .Cells(1).Range.Text = "Testing"
    End With
End With

HTH


Quote:
> 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, but if the
> NEXT row has a much samller 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.

> 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.  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 in checking the total table height.  Advice on ways to get around
> this would be helpful 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 inches * 72 to 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

> ----------------------------

> Please post a repsonse here, and by email to

> Thanks,
> Corey



Wed, 20 Jul 2005 06:07:32 GMT  
 adding a table row, without setting same defaults as previous row
Quote:

> Hi Corey,

> Maybe the following does the trick for you??

> Dim oTbl As Table
> Set oTbl = ActiveDocument.Tables(1)

> With oTbl
>     .Rows.Add
>     With .Rows(.Rows.Count)
>         .Height = 12
>         .Cells(1).Range.Text = "Testing"
>     End With
> End With

> HTH

Thanks for the response Dave.  

Unfortunately that didn't help.  I've included below a copy of a new
pmessage I just posted which explains my problem better (I think).
I've changed the code so that it actually runs through a userform
(instead of just testing the code through a command button).  I hope
you can follow exactly what is going on by my description (hard to
explain in words, easier to see it in action)!

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

Hello.  Here's my problem:

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
a 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).  All the code (add row, check row height, insert data into
cells) is run through a command button on the userform.

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 (just
enough to fit one row of text), thus hiding some of the info in the
cells if the text has had to wrap.  Even the row with the column
headings is changed (once I add a second row - for a total of three
row).

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!). [Each time it is
the third row up is changed].  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 for that particular cell.  Some
rows may be small, some may be large.

Also, is there any other way I can limit the vertical size of the
table other than by 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 as defined, 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 "There are too many rows!  Start a new file!"
   End If

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

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

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 "There are too many rows!  Start a new file!"
   End If

   Unload frmDensities

End Sub

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

Regards,

Corey Dale

remove (nospam) for email



Tue, 26 Jul 2005 00:15:56 GMT  
 adding a table row, without setting same defaults as previous row
I managed a passable workaround for my problem, but it has created
another problem!

I changed my code as follows (to force each row back to a height rule
of auto):

.
.
.
.
If AddRowCheck1 < 200 Then    ' 4.5 inches * 70 to convert to points =
315

    ' add new row

    Set NewRow1 = myTable1.Rows.Add
    LastRow1 = myTable1.Rows.Count
    For d = 1 To LastRow1
         myTable1.Rows(d).HeightRule = wdRowHeightAuto
    Next d

    ' 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 "There are too many rows!  Start a new file!"
    LastRow1 = myTable1.Rows.Count

    ' added coding here to set all rows back to auto rule
    For d = 1 To LastRow1
         myTable1.Rows(d).HeightRule = wdRowHeightAuto
    Next d

End If
.
.
.
.

Now, the new problem that has been created is that it takes quite a
long time to always go back and change all the rows back to 'auto'
(the more # of rows, the longer it takes).

My problem stems from having to change the height rule to 'exactly' in
order to determine the actual height of each rule (in my total table
height check).  Does anyone have suggestions on how I can limit the
vertical size of my table, other than how I have done it above? Maybe
that could also be a topic all on its own.

Thanks,
Corey



Tue, 26 Jul 2005 23:32:05 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Add row to datatable from datagrid without leaving current row with cursor

2. row heights of table rows added by vba

3. Determining actual row height, or setting maximum row height

4. Err No rows at, duplicate table and row names

5. table row text will not show after row insert

6. Add Button to a row by row data display

7. How to read Access Table row by row?

8. View mdb tables rows by rows

9. adding rows without reloading the page

10. How to Create 2 column table and continue adding rows

11. Format of Rows Added to a Table

12. Adding Rows to a Table

 

 
Powered by phpBB® Forum Software