Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Jra,
You seem to have forgotten to attach the example
Avatar of JohnRobinAllen

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.