This topic describes the API operations that are related to the Selection object of text documents.
Selection
ActiveDocument.ActiveWindow.Selection
Obtains the current selection in a window or a pane. A selection represents either a selected (or highlighted) area in the document, or it represents the insertion point if nothing in the document is selected.
Only JS-SDK V1.1.10 and later support this feature.
There can be only one
Selectionobject per document window pane, and only oneSelectionobject in the entire application can be active.
Syntax
expression.ActiveDocument.ActiveWindow.Selectionexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; }
Methods
ActiveDocument.ActiveWindow.Selection.Copy()
You can copy the selection to the clipboard by using the Copy() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Copy()expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. await app.ActiveDocument.Range.SetRange(1, 10); // Copy the selection to the clipboard. await app.ActiveDocument.ActiveWindow.Selection.Copy(); // The selection is copied to the clipboard and can be pasted by using Ctrl+V. }
ActiveDocument.ActiveWindow.Selection.Delete()
You can delete the selection by using the Delete() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Delete({ Unit, Count })expression: the document type application object.
Parameters
Property
Type
Required
Description
Unit
Enum
Yes
Specifies the unit that you want to use. Only character and line are supported. For more information, see WdUnits.
Count
Number
No
The number of units that you want to delete. Valid value: 1. Default value: 1.
Example
async function example() { await instance.ready(); const app = instance.Application; // Delete the selection. app.ActiveDocument.ActiveWindow.Selection.Delete(1); }
ActiveDocument.ActiveWindow.Selection.GoTo()
You can jump to a position, such as a specified page or bookmark, by using the GoTo() method.
Only JS-SDK V1.1.10 and later support this feature.
Text documents are in dynamic layout. A long period of time is required to perform the jump in large documents. We recommend that you add
loadingto the script.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.GoTo(What, Which, Count, Name)expression: the document type application object.
Parameters
Property
Type
Required
Description
What
Number
No
The type of item to which the range or selection is moved. For more information, see WdGoToItem.
Which
Number
No
The item to which the range or selection is moved. For more information, see WdGoToDirection.
Count
Number
No
The number of the item in the document. The default value is 1. Only positive integers are valid.
Name
String
No
The name of the bookmark. If you specify
wdGoToBookmarkfor the What parameter, you can specify the name of the bookmark.NoteIf the bookmark name that you specify and the value of Count do not match, the jump cannot be performed.
Return values
Returns the page or bookmark after the jump is performed.
Examples
Jump to a specified page
async function example() { await instance.ready(); const app = instance.Application; // Jump to a specified page. const page = await app.ActiveDocument.ActiveWindow.Selection.GoTo( app.Enum.WdGoToItem.wdGoToPage, app.Enum.WdGoToDirection.wdGoToAbsolute, 3, ); }Jump to a specified bookmark
async function example() { await instance.ready(); const app = instance.Application; // Add a bookmark. await app.ActiveDocument.ActiveWindow.Bookmarks.Add({ Name: 'Aliyun', Range: { Start: 1, End: 10, }, }); // Jump to a specified bookmark. // Method 1 await app.ActiveDocument.ActiveWindow.Selection.GoTo( app.Enum.WdGoToItem.wdGoToBookmark, app.Enum.WdGoToDirection.wdGoToAbsolute, undefined, 'Aliyun', ); // Method 2 await app.ActiveDocument.ActiveWindow.Selection.GoTo({ What: app.Enum.WdGoToItem.wdGoToBookmark, Which: app.Enum.WdGoToDirection.wdGoToAbsolute, Name: 'Aliyun', }); }
ActiveDocument.ActiveWindow.Selection.Information()
You can obtain information about the selection by using the Information() method.
Only JS-SDK V1.1.10 and later support this feature.
Text documents are in dynamic layout. The exact number of pages can be obtained only by scrolling to the bottom of the document.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Information(WdInformation)expression: the document type application object.
Parameters
Property
Type
Required
Description
WdInformation
Enum
Yes
Specifies the type of information about the selection or range that you want to return. For more information, see WdInformation.
Return values
Returns a Number that indicates the current page number.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the current page number. const currentPage = await app.ActiveDocument.ActiveWindow.Selection.Information(app.Enum.WdInformation.wdActiveEndPageNumber); console.log(currentPage ); }
ActiveDocument.ActiveWindow.Selection.InsertAfter()
You can insert text after the selection by using the InsertAfter() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.InsertAfter({ Text })expression: the document type application object.
Parameters
Property
Type
Required
Description
Text
String
Yes
The text that you want to insert.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert text after the selection await app.ActiveDocument.ActiveWindow.Selection.InsertAfter({ Text: 'Text', }); }
ActiveDocument.ActiveWindow.Selection.InsertBreak()
You can insert a break after the selection by using the InsertBreak() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.InsertBreak({ Type })expression: the document type application object.
Parameters
Property
Type
Required
Description
Type
Enum
No
The type of break that you want to insert. Default value:
wdPageBreak. For more information, see WdBreakType.Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a break after the selection. await app.ActiveDocument.ActiveWindow.Selection.InsertBreak(); }
ActiveDocument.ActiveWindow.Selection.InsertParagraph()
You can replace the selection with a new paragraph by using the InsertParagraph() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.InsertParagraph()expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Replace the selection with a new paragraph. await app.ActiveDocument.ActiveWindow.Selection.InsertParagraph(); }
ActiveDocument.ActiveWindow.Selection.MoveDown()
You can move the selection down by using the MoveDown() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.MoveDown({ Unit, Count, Extend })expression: the document type application object.
Parameters
Property
Type
Required
Description
Unit
Enum
No
The unit by which you want to move the selection. Default value:
wdLine. For more information, see WdUnits.NoteIf the value is
wdWindow, you can move the selection by only one unit regardless of the value of Count.Otherwise, you can specify the number of units by which you want to move the selection by specifying Count.
Count
Number
No
The number of units by which you want to move the selection. Default value: 1.
Extend
Enum
No
Specifies how you want to move the selection. Default value:
wdMove. For more information, see WdMovementType.NoteIf the value is
wdMove, the selection is collapsed to the endpoint and moved downward.If the value is
wdExtend, the selection is extended downward.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the selection downward. await app.ActiveDocument.ActiveWindow.Selection.MoveDown(); }
ActiveDocument.ActiveWindow.Selection.MoveLeft()
You can move the selection to the left by using the MoveLeft() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.MoveLeft({ Unit, Count, Extend })expression: the document type application object.
Parameters
Property
Type
Required
Description
Unit
Enum
No
The unit by which you want to move the selection. Default value:
wdCharacter. For more information, see WdUnits.Count
Number
No
The number of units by which you want to move the selection. Default value: 1.
Extend
Enum
No
Specifies how you want to move the selection. Default value:
wdMove. For more information, see WdMovementType.NoteIf the value is
wdMove, the selection is collapsed to the endpoint and moved to the left.If the value is
wdExtend, the selection is extended to the left.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the selection to the left. await app.ActiveDocument.ActiveWindow.Selection.MoveLeft(); }
ActiveDocument.ActiveWindow.Selection.MoveRight()
You can move the selection to the right by using the MoveRight() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.MoveRight({ Unit, Count, Extend })expression: the document type application object.
Parameters
Property
Type
Required
Description
Unit
Enum
No
The unit by which you want to move the selection. Default value:
wdCharacter. For more information, see WdUnits.Count
Number
No
The number of units by which you want to move the selection. Default value: 1.
Extend
Enum
No
Specifies how you want to move the selection. Default value:
wdMove. For more information, see WdMovementType.NoteIf the value is
wdMove, the selection is collapsed to the endpoint and moved to the right.If the value is
wdExtend, the selection is extended to the right.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the selection to the right. await app.ActiveDocument.ActiveWindow.Selection.MoveRight(); }
ActiveDocument.ActiveWindow.Selection.MoveUp()
You can move the selection up by using the MoveUp() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.MoveUp({ Unit, Count, Extend })expression: the document type application object.
Parameters
Property
Type
Required
Description
Unit
Enum
No
The unit by which you want to move the selection. Default value:
wdLine. For more information, see WdUnits.NoteIf the value is
wdWindow, you can move the selection by only one unit regardless of the value of Count.Otherwise, you can specify the number of units by which you want to move the selection by specifying Count.
Count
Number
No
The number of units by which you want to move the selection. Default value: 1.
Extend
Enum
No
Specifies how you want to move the selection. Default value:
wdMove. For more information, see WdMovementType.NoteIf the value is
wdMove, the selection is collapsed to the endpoint and moved upward.If the value is
wdExtend, the selection is expanded upward.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the selection upward. await app.ActiveDocument.ActiveWindow.Selection.MoveUp(); }
ActiveDocument.ActiveWindow.Selection.TypeBackspace()
You can delete the character that precedes the selection by using the TypeBackspace() method.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.TypeBackspace()expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the selection downward. await app.ActiveDocument.ActiveWindow.Selection.MoveDown(); // Delete the character that precedes the selection. await app.ActiveDocument.ActiveWindow.Selection.TypeBackspace(); }
Properties
ActiveDocument.ActiveWindow.Selection.Cells
You can obtain all cells in a selection by using the Cells property.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Cellsexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; // Obtain all cells. const font = await selection.Cells; }
ActiveDocument.ActiveWindow.Selection.Font
You can obtain the character formatting of the selection by using the Font property.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Fontexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; // Obtain the character formatting of the selection. const font = await selection.Font; }
ActiveDocument.ActiveWindow.Selection.InlineShapes
You can obtain all InlineShape objects in the selection by using the InlineShapes property.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.InlineShapesexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; // Obtain all InlineShape objects. const font = await selection.InlineShapes; }
ActiveDocument.ActiveWindow.Selection.ParagraphFormat
You can obtain the paragraph settings of the selection by using the ParagraphFormat property.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.ParagraphFormatexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; // Obtain the paragraph settings. const paragraphFormat = await selection.ParagraphFormat; }
ActiveDocument.ActiveWindow.Selection.Range
You can obtain the portion of a document that is contained in the selection by using the Range property.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Rangeexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; // Obtain a portion of the document. const paragraphFormat = await selection.ParagraphFormat; }
ActiveDocument.ActiveWindow.Selection.Rows
You can obtain all table rows in the selection by using the Rows property.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Rowsexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; // Obtain all table rows. const paragraphFormat = await selection.ParagraphFormat; }
ActiveDocument.ActiveWindow.Selection.Tables
You can obtain all tables in the selection by using the Tables property.
Only JS-SDK V1.1.10 and later support this feature.
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Tablesexpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain a Selection object. const selection = await app.ActiveDocument.ActiveWindow.Selection; // Obtain all tables. const paragraphFormat = await selection.ParagraphFormat; }