Fredrik Karlsson
Fredrik Karlsson

Reputation: 19

VBA Word Assign Table Header rows

I have a table that is pasted (pasteandformat) from Excel. Now I need the top rows, 1 and 2, to show on top on every page. The problem is that I can´t get it to work using VBA code, but doing it manually works.

I´ve looked around the internet, MSDN, and the objectlibrary in VBA, but I can seem to find how to do it, with code, the same way as if done manualy.

Sub test()
    Dim tbl As Table

    Set tbl = ActiveDocument.Tables(1)

    tbl.Rows(1).HeadingFormat = True
End Sub

Runtime Error: 5991 Cant access specific rows in the selection due to the table containing vertical merged cells

But if I do it manually, entering the Table Properties dialog box, tab Row, and select row 1 and then tick the option Repeat the top row on every page, it works just fine.

Upvotes: 0

Views: 2471

Answers (1)

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

As you have vertically merged cells in the first two rows you need to select the rows before setting the HeadingFormat

ActiveDocument.Tables(1).Cell(1, 1).Select
With Selection
  .MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
  .Expand wdRow
  .Rows.HeadingFormat = True
End With

Upvotes: 0

Related Questions