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,

Source: Internet
Author: User

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 ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.