2016-05-30 61 views
0

我目前正试图产生Selection.SelectCell方法的相反效果。C#中 - 如何取消Word中的表格单元格

这里是我的代码:

public void getCellContent() 
{ 
    Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection; 

    currentSelection.SelectCell(); 
    newKey = currentSelection.Text; //global variable 
    currentSelection.Collapse(); 
} 

Collapse方法将光标定位到单元的开始。 即使我将折叠方向参数设置为最后,光标也会进入下一个单元格。

我的目的是能够保存一个单词表单元格的内容,每次我输入它(没有改变单元格)。

最好的问候,

Chefty。

回答

1

您将无法将光标置于Word表格单元格内的文本末尾。不是没有从细胞移动到另一个。

做到这一点的唯一办法是使用下面的代码:

currentSelection.Collapse(Word.WdCollapseDirection.wdCollapseEnd); 
currentSelection.MoveLeft(Word.WdUnits.wdCharacter, 1); 

不要这样做。考虑到每当你输入一个字符时都会调用它,它不会快速和有效,并且最终你会在下一个单元格中输入字符。

当你自己改变选择时,你应该只保存一次你的单元格的内容。当你输入你的第一个字符,请使用以下代码:

currentCell = currentSelection.Range.Cells[1]; //index always starts at 1, not 0 

而当你点击另一个单元格(你也许不得不使用Win32 API的赶上这样的活动),不要忘记做:

currentCell.Select(); 
string currentCellContent = currentSelection.Text; 

之后,你仍然通过执行一个保存每个编辑最终有一个选定的单元格,仍然需要使用currentSelection.Collapse(),但至少你有你的单元格的内容,还有这个。

+1

谢谢,它的工作原理! – Chefty

相关问题