C# - 如何在 Word 中取消选择 table 单元格
C# - How to deselect table cell in Word
我目前正在尝试产生与 Selection.SelectCell 方法相反的效果。
这是我的代码:
public void getCellContent()
{
Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
currentSelection.SelectCell();
newKey = currentSelection.Text; //global variable
currentSelection.Collapse();
}
折叠方法将光标设置到单元格的开头。
即使我将 Collapse direction 参数设置到最后,光标也会转到下一个单元格。
我的目的是每次在单元格中输入时都能够保存单词 table 单元格的内容(无需更改单元格)。
此致,
厚颜无耻。
您将无法将光标设置在 Word 单元格内文本的末尾 table。不是没有从一个单元格移动到另一个单元格。
这样做的唯一方法是使用以下代码:
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()
,但至少您已经获得了单元格的内容,并且这是通过每次编辑执行一次保存来实现的。
我目前正在尝试产生与 Selection.SelectCell 方法相反的效果。
这是我的代码:
public void getCellContent()
{
Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
currentSelection.SelectCell();
newKey = currentSelection.Text; //global variable
currentSelection.Collapse();
}
折叠方法将光标设置到单元格的开头。 即使我将 Collapse direction 参数设置到最后,光标也会转到下一个单元格。
我的目的是每次在单元格中输入时都能够保存单词 table 单元格的内容(无需更改单元格)。
此致,
厚颜无耻。
您将无法将光标设置在 Word 单元格内文本的末尾 table。不是没有从一个单元格移动到另一个单元格。
这样做的唯一方法是使用以下代码:
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()
,但至少您已经获得了单元格的内容,并且这是通过每次编辑执行一次保存来实现的。