VBA Word Changing table row and column size in

2019-09-13 19:48发布

nested table in WordI have a template which is essentially a 4-row column in Work. Each cell marked with a bookmark. I've copied a table from Excel to Word into one of these bookmarks (inside on of the cells). Now I'm trying to format this 'nested table' to make it fit according to my desired column widths (for certain columns) but I'm really struggling with the syntax. Additionally, in the table, the first row contains some merged cells (Some are merged to the cell in the adjacent column and some in the row below).

The code I was trying:

With wd.Tables(2)
    .Columns(2).Width = 20
End With

But I keep getting "Run-time error '5941': The requested member of the collection does not exist." Does this mean I am not indexing it properly?

Tabels(2) is meant to refer to the 'nested table' within the larger single column of 4 rows cells.

How do index it properly/find its index? And how would I change the widths when I have merged cells? Would I need to: divide them first>adjust width>re-merge? Also, I'm doing this in VBA Word, if I reference 'Microsoft Word xx.0 Object Library' in excel VBA can I do this in excel?

1条回答
Anthone
2楼-- · 2019-09-13 20:41

I have recreated a nested table like the one in the screen shot; i.e. 1 column with 4 rows, then a nested 14 column/10 row table in row 3.

The following code works just fine for me:

Sub AccessNestedTable()
Dim tbl As Table, tbl2 As Table
Dim wd As Document

Set wd = ActiveDocument

Set tbl = wd.Tables(1)
Set tbl2 = tbl.Tables(1)

With tbl2
    .Columns.Width = 20
End With

End Sub
查看更多
登录 后发表回答