This topic describes how to obtain a selection object, move a selection, and delete the content of a selection when you use a text document.
Obtain a selection object
Obtain the selected object in a specified window or pane.
A selection represents either a selected or highlighted range in the document, or the insertion point if nothing in the document is selected.
Syntax
Expression.ActiveDocument.ActiveWindow.SelectionExpression: the document type application object.
Each document window or pane can have only one
selectionobject. Each application can have only one activeselectionobject.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the selection object. const selection = await app.ActiveWindow.Selection; }
Delete the content of a selection
Delete the content of a specified selection.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.Delete(WdUnits)Expression: the document type application object.
Parameters
Parameter
Type
Description
WdUnits
Enum
The unit by which the selection is to be deleted. Valid values of
Enum.WdUnits:1 or wdCharacter: a character.
2 or wdWord: a word.
3 or wdSentence: a sentence.
4 or wdParagraph: a paragraph.
5 or wdLine: a line.
6 or wdStory: a story.
7 or wdScreen: the screen dimensions.
8 or wdSection: a section.
9 or wdColumn: a column.
10 or wdRow: a row.
11 or wdWindow: a window.
12 or wdCell: a cell.
13 or wdCharacterFormatting: character formatting.
14 or wdParagraphFormatting: paragraph formatting.
15 or wdTable: a table.
16 or wdItem: the selected item.
Example
async function example() { await instance.ready(); const app = instance.Application; // Delete the content of the selection. app.ActiveDocument.ActiveWindow.Selection.Delete(1); }
Move a selection down
Move a selection down.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.MoveDown()Expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the cursor down. await app.ActiveDocument.ActiveWindow.Selection.MoveDown(); }
Move a selection up
Move a selection up.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.MoveUp()Expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the cursor up. await app.ActiveDocument.ActiveWindow.Selection.MoveUp(); }
Move a selection to the left
Move a selection to the left.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.MoveLeft()Expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the cursor to the left. await app.ActiveDocument.ActiveWindow.Selection.MoveLeft(); }
Move a selection to the right
Move a selection to the right.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.MoveRight()Expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the cursor to the right. await app.ActiveDocument.ActiveWindow.Selection.MoveRight(); }
Obtain the selection range object
Obtain a range object that represents the portion of a document that is contained in the specified object.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.RangeExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the selection object. const selection = await app.ActiveWindow.Selection; // Obtain the selection range object. const range = await selection.Range; }
Change a selection range
Change a specified selection range.
Syntax
Expression.ActiveDocument.Range(Start, End).SetRange({ Start, End })orExpression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.SetRange({ Start, End })Expression: the document type application object.
Parameters
Parameter
Type
Required
Description
Start
Number
Yes
The start position of the selection.
End
Number
Yes
The end position of the selection.
Examples
Example 1
async function example() { await instance.ready(); const app = instance.Application; // Obtain a selection range. const range = await app.ActiveDocument.Range(0, 10); // Set the selection range. await range.SetRange(10, 20); }Example 2
async function example() { await instance.ready(); const app = instance.Application; // Obtain the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Obtain the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Obtain the range object of the cell. const range = await cellOne.Range; // Obtain the font properties of the cell. const font = await range.Font; // Modify the selection range. const newRange = await range.SetRange({ Start: 1, End: 10, }); const newText = await newRange.Text; // Obtain the text in the new range. console.log(newText); }