Make rows in a Word table the same height with VBA code.

I have a seven-page table in Word created by VBA code. (See the attached, 1-page excerpt.) After I create the table, the rows can have widely varying heights. I’d like to add code that would make the rows preferred height be the same for all cells that have just one line but, if necessary, I’d like the height to expand if a cell has two rows.
     I’m currently using the following five lines of code, downloaded from some site on the Internet, to adjust the height of the table:

     Sub SetRowHeight()
         Dim tr As Row
         For Each tr In ActiveDocument.Tables(1).Rows
              'HeightRule is an enumeration with three values. By specifying
              'wdRowHeightExactly for the value, we are forcing an exact height for table
              'rows.
              '     You can also specify wdRowHeightAuto (the default, row height is adjusted to
              'accomodate the tallest value in the row) or wdRowHeightAtLeast (specify a
              'minimum value. This option is selected if you manually adjust the height of
              'a row.)

              tr.HeightRule = wdRowHeightExactly

             'Note that row height is in points, not inches (the Table Tools Layout tab in the UI
             'uses inches. There are 72 points per inch.)

             tr.Height = 13
          Next tr
     End Sub

     That code cuts off the bottom of the title bar in the attached table and also does not allow expansion of height when necessary. To show that, I copied and pasted the first French word in the list a few extra times in the cell to see if the table could adjust itself when a cell had two lines. The table does not adjust to fit the line, but you can force an expansion manually, and you will then see there is more text in the cell in the second row (line numbered 1) than first displayed.
     If I use the wdRowHeightAuto argument, that does not pull the table height down to something reasonable. The table before I run the code has a consistent row height of about five lines of text, even though very few lines have more than one line of text. It appears that wdRowHeightAuto is useless without some further help.
     Is there some VBA code that can set a standard, preferred height for rows in a table? If not, I can still use the above code and then adjust rows by hand after I make the table.
     The table is also giving problems in some subsequent pages in that the bottom of the last line of the table at the bottom of a page is sometimes getting cut off in the middle of a row. For space reasons, I did not include a table that shows that problem, but I think a solution to my original problem will fix that second problem.
     Thanks, as always, for help. Whenever I find an impossible problem, someone in EE usually solves the impossible problem.

     Jra in Priddis, Alberta
JohnRobinAllenRetired professor of FrenchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
Jra,
You seem to have forgotten to attach the example
0
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I did not forget to attach the sample, but I forgot how to upload a file. You have to do more than simply select the file. I hope this time is a charm.
    j.r.
Sample-Table.doc
0
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I see that the file I uploaded did not have the first French word copied and pasted in its cell a few times to increase the lines.
Sample-Table-2.doc
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

GrahamSkanRetiredCommented:
The Row.Height property doesn't return the actual row height, so we have to use the Information property of the range.

Sub StandardiseRowHeights()
    Dim rw As Row
    Dim tbl As Table
    Dim sTop_ As Single
    Dim sBottom As Single
    Dim rng As Range
    Dim sngTop As Single
    Dim sngBottom As Single
    Dim r As Integer
    Dim sngMax As Single
    
    'Dim pt As WdProtectionType
    'pt = ActiveDocument.ProtectionType
    'ActiveDocument.Unprotect
    
    Set tbl = ActiveDocument.Tables(1)
    
    'allow each tow to resize according to the contents
    For Each rw In tbl.Rows
        rw.HeightRule = wdRowHeightAuto
    Next rw

    'Find the actual height of each row and take the maximum value
    For r = 1 To tbl.Rows.Count
        sngTop = tbl.Rows(r).Range.Information(wdVerticalPositionRelativeToPage)
        If r < tbl.Rows.Count Then
            sngBottom = tbl.Rows(r + 1).Range.Information(wdVerticalPositionRelativeToPage)
        Else 'last row, bottom of row =  bottom of row
            Set rng = tbl.Range
            rng.Collapse wdCollapseEnd
            sngBottom = rng.Information(wdVerticalPositionRelativeToPage)
        End If
        If sngMax < sngBottom - sngTop Then
            sngMax = sngBottom - sngTop
        End If
    Next r
    
    'set each row to the hight of the maximum
    For Each rw In tbl.Rows
        rw.HeightRule = wdRowHeightExactly
        rw.Height = sngMax
    Next rw
  End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnRobinAllenRetired professor of FrenchAuthor Commented:
Graham Skan's solution is fascinating but in the end does more than what I want. My main objection is that I want each row height to be as small as possible so that I can get the six- or seven-page table printed on as few pages as possible. His solution proposed makes all rows the same height, large enough to handle the cell that needs the most height but far larger than what the row needs.

Apparently what I wanted was far easier to get than I had thought. The first three lines of Graham’s code suffice to do it perfectly.
    For Each rw In tbl.Rows
        rw.HeightRule = wdRowHeightAuto
    Next rw

Once again, many thanks for a beautiful solution.

J.R. in Priddis
0
GrahamSkanRetiredCommented:
Thanks J.R. Making things more complicated than necessary seems to be one of my talents.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.