vbams-wordrow-height

Adjusting row height if tables in word using vba


I have a template based on the repetition of two pages with two different tables. I am working on a macro to adjust the row heights of these tables throughout the document so that the row heights are the same. Sometimes the tables stay on the page, sometimes it does overflows continuously onto a new page.

I have been trying a few different ways and the below is the closest I have come to getting it to work. Below gets the actual row height by looking at the position against the document. The issue I am having is that the tables are crossing pages and so keeps showing an error when it gets to a row on the next page. The error is 'The measurement must be between 0 pt and 1584 pt.'

This is the code I am currently using:

A = 1
B = 2

While B <= ActiveDocument.Tables.Count

Set T1 = ActiveDocument.Tables(A)
Set T2 = ActiveDocument.Tables(B)

Set R1 = T1.Rows
Set R2 = T2.Rows

Set C1 = T1.Columns
Set C2 = T2.Columns

For i = 1 To R1.Count()
   If i = R1.Count() Then
   Else
      H1 = T1.Rows(i + 1).Range.Information(wdVerticalPositionRelativeToPage) _
    - T1.Rows(i).Range.Information(wdVerticalPositionRelativeToPage)
      H2 = T2.Rows(i + 1).Range.Information(wdVerticalPositionRelativeToPage) _
    - T2.Rows(i).Range.Information(wdVerticalPositionRelativeToPage)

      If H1 > 0 Or H1 < 1584 Or H2 > 0 Or H2 < 1584 Then
         If H1 > H2 Then
            R2(i).Height = H1
         Else
            R1(i).Height = H2
         End If
      End If
   End If

Next

A = A + 1
B = B + 2
Wend

I have also tried setting the height using the below, which doesn't work in this case as it only gets the default height of the row and not the actual height.

H1 = R1(i).Height
H2 = R2(i).Height

Thank you for any help in advance.


Solution

  • Thank you to everyone who helped. I ended up resolving this by using the following code and making the page of the document extremely long. Not ideal, but worked.

    Sub rowHeight()
    A = 2
    B = 4
    
    While B <= ActiveDocument.Tables.Count
    
    Set T1 = ActiveDocument.Tables(A)
    Set T2 = ActiveDocument.Tables(B)
    
    Set r1 = T1.Rows
    Set r2 = T2.Rows
    
    Set C1 = T1.Columns
    Set C2 = T2.Columns
    
    On Error Resume Next
    For i = 1 To r1.Count()
    
    If i = r1.Count() Then
    Else
    H1 = T1.Rows(i + 1).Range.Information(wdVerticalPositionRelativeToPage) _
        - T1.Rows(i).Range.Information(wdVerticalPositionRelativeToPage)
    H2 = T2.Rows(i + 1).Range.Information(wdVerticalPositionRelativeToPage) _
        - T2.Rows(i).Range.Information(wdVerticalPositionRelativeToPage)
    'H1 = R1(i).Height
    'H2 = R2(i).Height
    If H1 > 0 & H1 < 1584 & H2 > 0 & H2 < 1584 Then
    If H1 > H2 Then
    r2(i).Height = H1
    Else
    r1(i).Height = H2
    End If
    End If
    End If
    
    Next
    
    A = A + 4
    B = B + 4
    Wend
    
    End Sub