1

I'm trying to build a macro for Word that will go through every table and remove any line spacing from the text and then resize the tables to match.

I have the following code:

For Each tbl In ActiveDocument.Tables
    For Each r In tbl.Rows
        r.SetHeight RowHeight:=0.1, HeightRule:=wdRowHeightAtLeast
    Next
Next

However this returns

Run-time error '5991': "Cannot access individual rows in this collection because the table has vertically merged cells."

on the line For Each r In tbl.Rows

The merged cells are in the two-row-header of the table so I'm happy to ignore them in the process. How then would I do a For Each that skips the first two values of tbl.Rows?

Daniel V
  • 1,305
  • 7
  • 23
  • 1
    You could try catching the error and just ignoring the line on the error. See https://stackoverflow.com/questions/1038006/good-patterns-for-vba-error-handling for an example of this. – OpiesDad Dec 09 '19 at 21:51
  • I've tried putting `On Error Resume Next` in at various points within the code however it either skips the table altogether or the bug still appears. It seems that the issue is not setting the row height for a merged cell but rather in setting aside the list of values for `r`. – Daniel V Dec 09 '19 at 21:57
  • If you want to ignore the first couple of rows then you can do something like `For i = 3 to tbl.Rows.Count` then work with `tbl.Rows(i)` – Tim Williams Dec 09 '19 at 22:06
  • 1
    Any reason you couldn't do: `tbl.Rows.SetHeight RowHeight:=0.1, _ HeightRule:=wdRowHeightAtLeast`? – OpiesDad Dec 09 '19 at 22:09

1 Answers1

1

Could you set the rows for the whole table at once?

For Each tbl In ActiveDocument.Tables
    tbl.Rows.SetHeight RowHeight:=0.1, HeightRule:=wdRowHeightAtLeast
Next
OpiesDad
  • 3,385
  • 2
  • 16
  • 31