如何在Word表格中选择矩形单元格区域
给定类似
Table table;
Cell cell_1 = table.Cell(2,2);
Cell cell_2 = table.Cell(4,4);
我想从 cell_1 到 cell_2 选择(或突出显示)的内容(就像手动操作时的操作方式一样)。
我最初认为执行以下操作会起作用:
Selection.MoveRight(wdUnits.wdCell, numCells, WdMovementType.wdExtend)
但是根据 http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.moveright%28v=office.11%29.aspx 在备注下,使用 wdCells 作为单位会将 WdMovementType 默认为 wdMove,我想不出解决方法。
Given something like
Table table;
Cell cell_1 = table.Cell(2,2);
Cell cell_2 = table.Cell(4,4);
I want to select (or highlight) from cell_1 to cell_2 (like how you would if you were doing it by hand).
I originally thought that doing the following would work:
Selection.MoveRight(wdUnits.wdCell, numCells, WdMovementType.wdExtend)
But according to http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.moveright%28v=office.11%29.aspx under remarks, using wdCells as the Unit will default the WdMovementType to wdMove, and I can't think of a workaround.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我发现的解决该问题的方法。这不是最有效的方法,并且
如果表格中包含合并单元格则不起作用
。我发现您可以选择起始单元格的范围,然后通过以单元格为单位移动来扩展范围的终点。通过发现要选择的区域的起点和终点之间的单元格数量,您可以迭代这些单元格步骤数。下面是一般代码:Here is a workaround I've found to the problem. It isn't the most efficient way, and it
doesn't work if the table has merged cells in it
. I've discovered that you can select the range of your start cell, and then expand the end point of the range by moving by in units of cells. By discovering the number of cells between the start and end point of the region you want selected, you can iterate those number of cell steps. Here's the general code for that below:一种更简单的方法是使用
Document.Range
方法在矩形的两个角之间创建一个范围。这对于合并单元格同样有效。注意:可以使用此表达式返回的范围来操作表格的内容而不选择它,但它不适用于合并单元格(在后一种情况下,请使用 cell.Merge(MergeTo)代码>)。
A much simpler way to do this is to use the
Document.Range
method to create a range between the two corners of the rectangle. This works equally well with merged cells.Note: One can use the range returned by this expression to manipulate the contents of the table without selecting it, but it doesn't work for merging the cells (in the latter case, use
cell.Merge(MergeTo)
).