C # control the positioning of the Word cursor to move to any row or the last row, and obtain the cursor position and other operations,
11. move the cursor position up, down, and left
Private void moveLeft ()
{
Object moveUnit = Microsoft. Office. Interop. Word. WdUnits. wdWord;
Object moveCount = 1;
Object moveExtend = Microsoft. Office. Interop. Word. WdMovementType. wdExtend;
ThisDocument. Application. Selection. MoveLeft (ref moveUnit, ref moveCount, ref MissingValue );
}
Private void moveRight ()
{
If (selection = null | selection! = Document. Application. Selection)
Selection = document. Application. Selection;
Object dummy = System. Reflection. Missing. Value;
Object count = 1;
Object Unit = Word. WdUnits. wdCharacter;
Selection. MoveRight (ref Unit, ref count, ref dummy );
}
12. Obtain the current cursor position
Public void GetCursor ()
{
If (selection = null | selection! = Document. Application. Selection)
Selection = document. Application. Selection;
Object a = selection. get_Information (Word. WdInformation. wdFirstCharacterLineNumber );
Object B = selection. get_Information (Word. WdInformation. wdFirstCharacterColumnNumber );
Object c = selection. get_Information (Word. WdInformation. wdActiveEndAdjustedPageNumber );
MessageBox. Show (a. ToString () + "row," + B. ToString () + "column," + c. ToString () + "page ");
}
13. Locate the specified row or relative row
/// <Summary>
/// Locate the specified row
/// </Summary>
/// <Param name = "lineNum"> row number </param>
Private void gotoAbsolutLine (int lineNum)
{
If (selection = null | selection! = Document. Application. Selection)
Selection = document. Application. Selection;
Object dummy = System. Reflection. Missing. Value;
Object what = Word. WdGoToItem. wdGoToLine;
Object which = Word. WdGoToDirection. wdGoToFirst;
Object count = lineNum;
Selection. GoTo (ref what, ref which, ref count, ref dummy );
}
/// <Summary>
/// Locate the relative row, for example, + 4
/// </Summary>
/// <Param name = "lineNum"> Number of rows </param>
Private void gotoOppositeLine (int lineNum)
{
If (selection = null | selection! = Document. Application. Selection)
Selection = document. Application. Selection;
Object dummy = System. Reflection. Missing. Value;
Object what = Word. WdGoToItem. wdGoToLine;
Object which;
If (lineNum <0)
Which = Word. WdGoToDirection. wdGoToPrevious;
Else
Which = Word. WdGoToDirection. wdGoToNext;
Object count = Math. Abs (lineNum );
Selection. GoTo (ref what, ref which, ref count, ref dummy );
}
14. Locate the last line of the document
Private void gotoLastLine (Document thisDocument)
{
Object dummy = System. Reflection. Missing. Value;
Object what = WdGoToItem. wdGoToLine;
Object which = WdGoToDirection. wdGoToLast;
Object count = 99999999;
ThisDocument. Application. Selection. GoTo (ref what, ref which, ref count, ref dummy );
}
15. Locate the first character
Private void gotoFirstCharacter ()
{
If (selection = null | selection! = Document. Application. Selection)
Selection = document. Application. Selection;
Int oldLine = 0;
GotoAbsolutLine (1 );
Object a = selection. get_Information (Word. WdInformation. wdFirstCharacterLineNumber); // obtain the current row number
While (oldLine! = Int. Parse (a. ToString () // right-click Until the cursor is no longer down.
{
OldLine ++;
MoveRight ();
A = selection. get_Information (Word. WdInformation. wdFirstCharacterLineNumber );
}
GotoAbsolutLine (int. Parse (a. ToString ()));
}
16. Locate the last character
Public void gotoLastCharacter ()
{
If (selection = null | selection! = Document. Application. Selection)
Selection = document. Application. Selection;
GotoLastLine ();
Object dummy = System. Reflection. Missing. Value;
Object count = 99999999;
Object Unit = Word. WdUnits. wdCharacter;
Selection. MoveRight (ref Unit, ref count, ref dummy );
}
21. Obtain row, column, and page information
Public string WordGetRCP ()
{
Selection = document. Application. Selection; // wd. Selection;
Object a = selection. get_Information (Word. WdInformation. wdFirstCharacterLineNumber );
Object B = selection. get_Information (Word. WdInformation. wdFirstCharacterColumnNumber );
Object c = selection. get_Information (Word. WdInformation. wdActiveEndAdjustedPageNumber );
Return a. ToString () + "," + B. ToString () + "," + c. ToString ();
}