确定Word单元格是否合并

发布于 2024-10-04 09:46:40 字数 619 浏览 0 评论 0原文

我需要以编程方式在 Word 2010 表中添加和删除行。不幸的是,表的标题包含合并的单元格,水平和垂直合并。使用 Row.Add 和 Row.Delete 方法时这会导致错误。我已经测试并发现我可以以编程方式删除合并的单元格 (Cell.Split),然后执行 .Add 和 .Delete 方法,然后恢复合并的单元格。我遇到的问题是确定哪些单元格被合并。

-------------------------
| 1,1 | 1,2 | 1,3 | 1,4 |
-------------------------
| 2,1 | 2,2 | 2,3 | 2,4 |
-------------------------

如果单元格 1,1 和 2,1 垂直合并,则访问 Table.Cell(2,1) 会引发错误。这很好。但是,如果单元格 1,1 和 1,2 水平合并,则访问 1,2 不会引发错误,但访问 1,4 则会引发错误。这意味着我无法确定哪些单元格被水平合并。

我想做的是让设计人员更改 Word 文档的外观和风格,但表中的数据是由人们使用 Web 应用程序填写的 SQL 查询填充的。

我的问题是,有没有办法确定表格的布局,以便我可以删除合并的单元格并在添加和删除行后重新创建它们?

谢谢, 亚伦

I need to programmatically add and remove rows from a Word 2010 table. Unfortunately the header of the table contains merged cells, both horizontally and vertically merged. That causes an error when using the Row.Add and Row.Delete methods. I have tested and discovered that I can programmically remove the merged cells (Cell.Split) and then do the .Add and .Delete methods and then restore the merged cells. The problem I'm having is identifying which cells that are merged.

-------------------------
| 1,1 | 1,2 | 1,3 | 1,4 |
-------------------------
| 2,1 | 2,2 | 2,3 | 2,4 |
-------------------------

If cells 1,1 and 2,1 are vertically merged then accessing Table.Cell(2,1) throws an error. Which is good. But, if cells 1,1 and 1,2 are horizontally merged, accessing 1,2 does not throw an error but accessing 1,4 does. Which means that I can't determine which cells are horizontally merged.

What I'm trying to do is let the design people change the look and feel of a Word document but the data in the table is populated by an SQL query that people fill in using a web app.

My question, is there a way to determine the layout of a table so that I can remove the merged cells and recreate them after adding and removing rows?

Thanks,
Aaron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗梦空间 2024-10-11 09:46:40

在处理可能包含合并单元格的表时,您始终必须从 CELL(1,1) 开始,并使用 CELL.NEXT 逐步执行单元格(Next 是 CELL 对象的属性)

如果您这样做,您可以询问什么“当前”单元格所在的行和列。您不能直接在具有合并单元格的表格中按行列引用单元格,否则您将收到您提到的错误。

然后继续直到 NEXT 什么也不返回。

When dealing with tables that might contain merged cells, you always have to start with CELL(1,1) and step through the cells using CELL.NEXT (Next is a property of the CELL object)

If you do that, you can interrogate what row and column the "current" cell is. You +can not+ reference cells by row col directly in tables with merged cells or you'll get the error you mention.

Then just continue through till NEXT returns nothing.

爱本泡沫多脆弱 2024-10-11 09:46:40

您必须推迟合并,直到处理完所有记录为止。

Word 不能很好地与表格配合使用。

我建议您创建一个 Excel 工作簿,并将 Excel 工作簿中的工作表链接到 Word 文档。 Excel 最擅长处理表格。

You will have to put off merging until you have dealt with all of the records.

Word doesn't work well with tables.

I suggest you create an Excel Workbook, and link the Worksheet from your Excel Workbook to your Word Document. Excel deals best with tables.

不羁少年 2024-10-11 09:46:40

好吧,我最终在表属性/替代文本/描述区域中描述了表的布局。然后在代码中读取该内容以拆分单元格,删除行,然后重新合并单元格。粗糙但有效。

Well I ended up describing the layout of the table in the Table Properties / Alt Text / Decription area. And then reading that in the code to split the cells, delete rows and then remerged the cells. Crude but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文