Selection

Updated at:

This topic describes the APIs for the Selection object in text documents.

Selection

ActiveDocument.ActiveWindow.Selection

Retrieves the current selection in a window or pane. A selection represents a highlighted area in a document. It can also represent the insertion point if nothing is selected in the document.

Important
  • This feature is supported in JavaScript Software Development Kit (JS-SDK) V1.1.10 and later.

  • Each document pane can have only one Selection object, and only one Selection object can be active in the entire application.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection

    Expression: Application object for the document type

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    }

Methods

ActiveDocument.ActiveWindow.Selection.Copy()

The Copy() method copies the selection to the clipboard.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Copy()

    Expression: Application object of the document type

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the Selection object.
      await app.ActiveDocument.Range.SetRange(1, 10);
    
      // Copy the selection to the clipboard.
      await app.ActiveDocument.ActiveWindow.Selection.Copy();
    
      // The content is copied to the clipboard. You can paste it using the Ctrl+V key combination.
    }

ActiveDocument.ActiveWindow.Selection.Delete()

The Delete() method deletes the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Delete({ Unit, Count })

    expression: A variable that represents a document type application object.

  • Parameters

    Property

    Data type

    Required

    Description

    Unit

    Enum

    Yes

    The measurement unit to use. Currently, only characters and lines are supported. For more information, see WdUnits.

    Count

    Number

    No

    The number of units to delete. Currently, only a value of 1 is supported. The default value is 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()

The GoTo() method moves the insertion point to a specific location, such as a page or a bookmark.

Important
  • This feature is supported in JS-SDK V1.1.10 and later.

  • Because text documents use a flow layout, moving the cursor in large documents can be time-consuming. We recommend that you add a loading transition effect.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.GoTo(What, Which, Count, Name)

    expression: A variable that represents a document type application object.

  • Parameters

    Property

    Data type

    Required

    Description

    What

    Number

    No

    The category of the item to which you want to move the range or selection. For more information, see WdGoToItem.

    Which

    Number

    No

    The position of the item to which you want to move the range or selection. 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 the What parameter is set to wdGoToBookmark, you can specify the bookmark name.

    Note

    If the specified bookmark name does not match the item number in the Count parameter, the operation fails.

  • Return value

    You can return to the previous page or a bookmark.

  • Example

    • Go to a specified page

      async function example() {
        await instance.ready();
      
        const app = instance.Application;
      
        // Go to a specified page.  
        const page = await app.ActiveDocument.ActiveWindow.Selection.GoTo(
          app.Enum.WdGoToItem.wdGoToPage,
          app.Enum.WdGoToDirection.wdGoToAbsolute,
          3,
        );
      }
    • Go 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,
          },
        });
      
        // Go to the 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()

The Information() method retrieves information about the specified selection.

Important
  • This feature is supported in JS-SDK V1.1.10 and later.

  • Because text documents use a flow layout, the total number of pages cannot be determined initially. The total page count is calculated only after you scroll to the end of the document.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Information(WdInformation)

    expression: A variable that represents a document type application object.

  • Parameters

    Property

    Data type

    Required

    Description

    WdInformation

    Enum

    Yes

    The type of information to return about a selection or range. For more information, see WdInformation.

  • Return value

    Returns a number that indicates the current page number.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the current page number.
      const currentPage = await app.ActiveDocument.ActiveWindow.Selection.Information(app.Enum.WdInformation.wdActiveEndPageNumber);
      console.log(currentPage );
    }

ActiveDocument.ActiveWindow.Selection.InsertAfter()

The InsertAfter() method inserts text after a selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.InsertAfter({ Text })

    Expression: Document Type Application Object

  • Parameters

    Property

    Data type

    Required

    Description

    Text

    String

    Yes

    The text 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()

The InsertBreak() method inserts a separator after a selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.InsertBreak({ Type })

    expression: A variable that represents a document type application object.

  • Parameters

    Property

    Data type

    Required

    Description

    Type

    Enum

    No

    The type of separator to insert. The default value is wdPageBreak. For more information, see WdBreakType.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Insert a separator after the selection.
      await app.ActiveDocument.ActiveWindow.Selection.InsertBreak();
    }

ActiveDocument.ActiveWindow.Selection.InsertParagraph()

The InsertParagraph() method replaces the selection with a new paragraph.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.InsertParagraph()

    Expression: Application object for the document type

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

The MoveDown() method moves the selection down.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.MoveDown({ Unit, Count, Extend })

    Expression: Document Type's Application Object

  • Parameters

    Property

    Data type

    Required

    Description

    Unit

    Enum

    No

    The measurement unit to use. The default value is wdLine. For more information, see WdUnits.

    Note

    • If this parameter is set to wdWindow, the selection moves only one unit, regardless of the value of Count.

    • For other values, you can set the Count parameter to move the selection by multiple units.

    Count

    Number

    No

    The number of units to move the selection. The default value is 1.

    Extend

    Enum

    No

    The way to move the selection. The default value is wdMove. For more information, see WdMovementType.

    Note
    • If this parameter is set to wdMove, the selection is collapsed to its endpoint and moved down.

    • If this parameter is set to wdExtend, the selection is extended down.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Move the selection down.
      await app.ActiveDocument.ActiveWindow.Selection.MoveDown();
    }

ActiveDocument.ActiveWindow.Selection.MoveLeft()

The MoveLeft() method moves the selection to the left.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.MoveLeft({ Unit, Count, Extend })

    Expression: Application object for the document type

  • Parameters

    Property

    Data type

    Required

    Description

    Unit

    Enum

    No

    The measurement unit to use. The default value is wdCharacter. For more information, see WdUnits.

    Count

    Number

    No

    The number of units to move the selection. The default value is 1.

    Extend

    Enum

    No

    The way to move the selection. The default value is wdMove. For more information, see WdMovementType.

    Note
    • If this parameter is set to wdMove, the selection is collapsed to its endpoint and moved to the left.

    • If this parameter is set to 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()

The MoveRight() method moves the selection to the right.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.MoveRight({ Unit, Count, Extend })

    Expression: Document-type application object

  • Parameters

    Property

    Data type

    Required

    Description

    Unit

    Enum

    No

    The measurement unit to use. The default value is wdCharacter. For more information, see WdUnits.

    Count

    Number

    No

    The number of units to move the selection. The default value is 1.

    Extend

    Enum

    No

    The way to move the selection. The default value is wdMove. For more information, see WdMovementType.

    Note
    • If this parameter is set to wdMove, the selection is collapsed to its endpoint and moved to the right.

    • If this parameter is set to 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()

The MoveUp() method moves the selection up.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.MoveUp({ Unit, Count, Extend })

    Expression: Document-type application object

  • Parameters

    Property

    Data type

    Required

    Description

    Unit

    Enum

    No

    The measurement unit to use. The default value is wdLine. For more information, see WdUnits.

    Note

    • If this parameter is set to wdWindow, the selection moves only one unit, regardless of the value of Count.

    • For other values, you can set the Count parameter to move the selection by multiple units.

    Count

    Number

    No

    The number of units to move the selection. The default value is 1.

    Extend

    Enum

    No

    The way to move the selection. The default value is wdMove. For more information, see WdMovementType.

    Note
    • If this parameter is set to wdMove, the selection is collapsed to its endpoint and moved up.

    • If this parameter is set to wdExtend, the selection is extended up.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Move the selection up.
      await app.ActiveDocument.ActiveWindow.Selection.MoveUp();
    }

ActiveDocument.ActiveWindow.Selection.TypeBackspace()

The TypeBackspace() method deletes the character that precedes the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.TypeBackspace()

    expression: A variable that represents a document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Move the selection down.
      await app.ActiveDocument.ActiveWindow.Selection.MoveDown();
    
      // Delete the character before the selection.
      await app.ActiveDocument.ActiveWindow.Selection.TypeBackspace();
    }

Properties

ActiveDocument.ActiveWindow.Selection.Cells

The Cells property returns all cells in the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Cells

    Expression: document-type application object

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    
      // Get all cells.
      const font = await selection.Cells;
    }

ActiveDocument.ActiveWindow.Selection.Font

The Font property returns the character format of the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Font

    expression: A variable that represents a document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    
      // Get the character format of the selection.
      const font = await selection.Font;
    }

ActiveDocument.ActiveWindow.Selection.InlineShapes

The InlineShapes property returns all inline shape objects in the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.InlineShapes

    Document Type Application Object Expression

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    
      // Get all inline shape objects.
      const font = await selection.InlineShapes;
    }

ActiveDocument.ActiveWindow.Selection.ParagraphFormat

The ParagraphFormat property returns the paragraph settings of the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.ParagraphFormat

    Expression: Application object of the document type

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    
      // Get the paragraph settings.
      const paragraphFormat = await selection.ParagraphFormat;
    }

ActiveDocument.ActiveWindow.Selection.Range

The Range property returns a Range object that represents the portion of a document that is contained in the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Range

    expression: A variable that represents a document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    
      // Get the document part.
      const paragraphFormat = await selection.ParagraphFormat;
    }

ActiveDocument.ActiveWindow.Selection.Rows

The Rows property returns all table rows in the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Rows

    expression: A variable that represents a document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    
      // Get all table rows.
      const paragraphFormat = await selection.ParagraphFormat;
    }

ActiveDocument.ActiveWindow.Selection.Tables

The Tables property returns all tables in the selection.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.ActiveWindow.Selection.Tables

    Expression: application object of the document type

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Get the Selection object.
      const selection = await app.ActiveDocument.ActiveWindow.Selection;
    
      // Get all tables.
      const paragraphFormat = await selection.ParagraphFormat;
    }