• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2266
  • Last Modified:

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
              '     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
  • 3
  • 3
1 Solution
You seem to have forgotten to attach the example
JohnRobinAllenAuthor 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.
JohnRobinAllenAuthor 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.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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
    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

JohnRobinAllenAuthor 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
Thanks J.R. Making things more complicated than necessary seems to be one of my talents.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now