2016-05-30 53 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

相關問題