All Products
Search
Document Center

Intelligent Media Management:Bookmark

Last Updated:Oct 29, 2024

This topic describes how to obtain bookmark objects, add a bookmark, and replace the content marked by a bookmark.

Obtain bookmark objects

  • Syntax

    Expression.ActiveDocument.Bookmarks

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain bookmark objects.
      const bookmarks = await app.ActiveDocument.Bookmarks;
    }

Add a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Add({ Name, Range })

    Expression: the document type application object.

  • Parameters

    Parameter

    Data type

    Required

    Description

    Name

    String

    Yes

    The name of the bookmark. The name must meet the following requirements:

    • No more than one word.

    • Cannot contain special characters such as spaces, digits, and periods (.).

    Range

    Object

    Yes

    The text area of the bookmark. You can add a bookmark to a collapsed area at an insertion point.

  • Description of the Range parameter

    Attribute

    Data type

    Required

    Description

    Start

    Number

    Yes

    The start point of the bookmark.

    End

    Number

    Yes

    The end point of the bookmark.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Obtain bookmark objects.
      const bookmarks = await app.ActiveDocument.Bookmarks;
    
      // Add a bookmark.
      await bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    }

Obtain the content marked by a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.GetBookmarkText(Name)

    Expression: the document type application object.

  • Parameters

    Parameter

    Data type

    Required

    Description

    Name

    String

    Yes

    The name of the bookmark.

  • Return value

    The content marked by the specified bookmark is returned.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Obtain bookmark objects.
      const bookmarks = await app.ActiveDocument.Bookmarks;
    
      // Add a bookmark.
      await bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Obtain the content marked by the bookmark.
      const bookmarkText = await bookmarks.GetBookmarkText('WebOffice');
      console.log(bookmarkText);
    }

Replace the content marked by a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.ReplaceBookmark(Data)

    Expression: the document type application object.

  • Parameters

    Parameter

    Data type

    Required

    Description

    Data

    Array.<Object>

    Yes

    The structure of the content for replacement.

  • Description of the Data parameter

    Attribute

    Data type

    Required

    Description

    name

    String

    Yes

    The name of the bookmark that you want to replace.

    type

    String

    Yes

    The type of the bookmark that you want to replace. The type of the bookmark is TEXT.

    value

    String

    Yes

    The new content marked by the bookmark.

  • Return value

    A value of the Boolean type is returned. If the return value is true, the replacement is successful. Otherwise, the replacement fails.

  • Example

    // Obtain all bookmarks.
    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Obtain bookmark objects.
      const bookmarks = await app.ActiveDocument.Bookmarks;
    
      // Add a bookmark.
      await bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Replace the content marked by the bookmark.
      const isReplaceSuccess = await bookmarks.ReplaceBookmark([
        {
          name: 'WebOffice',
          type: 'text',
          value: 'Replace the content marked by the bookmark',
        },
      ]);
      console.log(isReplaceSuccess); //true
    }

Obtain all bookmarks

  • Syntax

    Expression.ActiveDocument.Bookmarks.Json()

    Expression: the document type application object.

  • Response parameters

    Parameter

    Data type

    Description

    name

    String

    The name of the bookmark.

    begin

    Number

    The start point of the bookmark.

    end

    Number

    The end point of the bookmark.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Obtain all bookmarks.
      const bookmarks = await app.ActiveDocument.Bookmarks.Json();
      console.log(bookmarks);
    }

Check whether a bookmark exists

  • Syntax

    Expression.ActiveDocument.Bookmarks.Exists(Name)

    Expression: the document type application object.

  • Parameters

    Parameter

    Data type

    Required

    Description

    Name

    String

    Yes

    The name of the bookmark.

  • Return value

    A value of the Boolean type is returned. If the return value is true, the bookmark exists. Otherwise, the bookmark does not exist.

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Check whether the bookmark exists.
      const isExist = await app.ActiveDocument.Bookmarks.Exists('WebOffice');
      console.log(isExist); // true
    }

Obtain the number of bookmarks

  • Syntax

    Expression.ActiveDocument.Bookmarks.Count

    Expression: the document type application object.

  • Return value

    A value of the Number type is returned to indicate the number of bookmarks in the document.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Obtain the number of bookmarks.
      const count = await app.ActiveDocument.Bookmarks.Count;
      console.log(count);
    }

Obtain and set the bookmark sort order

  • Syntax

    Expression.ActiveDocument.Bookmarks.DefaultSorting = WdBookmarkSortBy

    Expression: the document type application object.

    For more information about WdBookmarkSortBy when you set the bookmark sort order, see Enum.WdBookmarkSortBy.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
    
      // Obtain the bookmark sort order.
      const sort = await app.ActiveDocument.Bookmarks.DefaultSorting;
    
      // Set the bookmark sort order.
      app.ActiveDocument.Bookmarks.DefaultSorting = 0;
    }

Operations on a single bookmark

Obtain a single bookmark object

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name)

    Expression: the document type application object.

  • Parameters

    Parameter

    Data type

    Required

    Description

    Name

    String

    Yes

    The name of the bookmark.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain bookmark objects.
      const bookmarks = await app.ActiveDocument.Bookmarks;
    
      // Add a bookmark.
      await bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Obtain a single bookmark object.
      await app.ActiveDocument.Bookmarks.Item('WebOffice');
    }

Delete a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).Delete()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Delete the bookmark.
      await app.ActiveDocument.Bookmarks.Item('WebOffice').Delete();
    }

Obtain the name of a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).Name

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Obtain the name of the bookmark.
      await app.ActiveDocument.Bookmarks.Item('WebOffice').Name;
    }

    Copy a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name1).Copy(Name2)

    Expression: the document type application object.

  • Parameters

    Parameter

    Data type

    Default value

    Required

    Description

    Name

    String

    Yes

    The name of the bookmark.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Copy the bookmark.
      await app.ActiveDocument.Bookmarks.Item('WebOffice').Copy('NewBookmark');
    }

    Select the content marked by a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).Select()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Select the content marked by the bookmark.
      await app.ActiveDocument.Bookmarks.Item('WebOffice').Select();
    }

    Check whether the content marked by a bookmark is empty

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).Empty

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Check whether the content marked by the bookmark is empty.
      const isEmpty = await app.ActiveDocument.Bookmarks.Item('WebOffice').Empty;
      console.log(isEmpty); // false
    }

    Check the start position for the content marked by a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).Start

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Check the start position for the content marked by the bookmark.
      const start = await app.ActiveDocument.Bookmarks.Item('WebOffice').Start;
      console.log(start); // 1
    }

    Check the end position for the content marked by a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).End

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Check the end position for the content marked by the bookmark.
      const end = await app.ActiveDocument.Bookmarks.Item('WebOffice').End;
      console.log(end); // 10
    }

    Obtain the range for the content marked by a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).Range

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Obtain the range for the content marked by the bookmark.
      await app.ActiveDocument.Bookmarks.Item('WebOffice').Range;
    }

    Obtain the type for the content marked by a bookmark

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).StoryType

    Expression: the document type application object.

    For more information about the type for the content marked by a bookmark, WdStoryType, see Enum.WdStoryType.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Obtain the type for the content marked by the bookmark.
      await app.ActiveDocument.Bookmarks.Item('WebOffice').StoryType;
    }

    Check whether the content marked by a bookmark is a column in a table

  • Syntax

    Expression.ActiveDocument.Bookmarks.Item(Name).Column

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Add a bookmark.
      await app.ActiveDocument.Bookmarks.Add({
        Name: 'WebOffice',
        Range: {
          Start: 1,
          End: 10,
        },
      });
    
      // Check whether the content marked by the bookmark is a column in a table.
      const column = await app.ActiveDocument.Bookmarks.Item('WebOffice').Column;
      console.log(column); // false
    }