Word VBA:含合并单元格的表格无法在右侧正确插入列的问题求助
Word VBA:含合并单元格的表格无法在右侧正确插入列的问题求助
我之前发过一篇帖子,尝试给带有合并单元格的表格末尾添加一列。虽然收到的一些建议给了我新方向,但这个问题至今还没彻底解决。
目前我能找到并选中表格每行的最后一个单元格,但添加单元格时,新单元格并不会出现在该行的右侧。
这是我正在处理的其中一个表格:
(原表格截图)
运行下面的test()代码后,我发现有些单元格的左侧被插入了新单元格,但并不是所有行都能正确添加:
(执行test()后的表格截图)
我特意切换到了草稿视图,这样能看到完整的表格大小。
Sub test() Dim t As Table, cel As Cell, colMax As Long Set t = ActiveDocument.Tables(1) For Each cel In t.Range.Cells cel.Select colMax = Selection.Information(wdMaximumNumberOfColumns) If cel.ColumnIndex = colMax Then Debug.Print "Cell ("; cel.RowIndex; ", "; cel.ColumnIndex; ") is the last cell in its row." cel.Select Selection.Cells.Add End If Next cel End Sub
用之前有用户建议的example()代码,结果也类似:新列被添加到了第一列的右侧,而不是表格的最后面:
(执行example()后的表格截图)
Sub example() Dim t As Table Set t = ActiveDocument.Tables(1) t.Columns.Add End Sub
另外,当我尝试用Columns.Last的时候,还直接抛出了运行时错误:
Run-time error '5992':
Cannot access individual columns in this selection because the table has mixed cell widths.
备注:内容来源于stack exchange,提问作者Nate




