VBA Word 更改表格行和列大小 [英] VBA Word Changing table row and column size in

查看:137
本文介绍了VBA Word 更改表格行和列大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Word 中的嵌套表格我有一个模板,它本质上是一个 4 行工作中的列.每个单元格都标有书签.我已将表格从 Excel 复制到 Word 到这些书签之一(在单元格内).现在我正在尝试格式化这个嵌套表"以使其适合我想要的列宽(对于某些列),但我真的在语法上苦苦挣扎.此外,在表格中,第一行包含一些合并的单元格(有些合并到相邻列中的单元格,有些合并到下一行中的单元格).

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).

我正在尝试的代码:

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

但我不断收到运行时错误‘5941’:请求的集合成员不存在."这是否意味着我没有正确索引它?

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) 是指在 4 行单元格的较大单列中的嵌套表格".

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

如何正确索引它/找到它的索引?合并单元格后如何更改宽度?我是否需要:先把它们分开>调整宽度>重新合并?另外,我正在 VBA Word 中执行此操作,如果我在 excel VBA 中引用Microsoft Word xx.0 对象库",我可以在 excel 中执行此操作吗?

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 列有 4 行,然后是第 3 行中嵌套的 14 列/10 行表.

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

这篇关于VBA Word 更改表格行和列大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆