How to set cursor position after insertText to word document?

Kishan Vaishnani 6 Reputation points
2023-01-23T12:41:56.34+00:00

I have created a Word add-in using Office JavaScript APIs

I called APIs to insert text into a document at the located cursor position

Below code, I am using

await Word.run(async (context) => {
  const paragraphs = context.document.body.paragraphs;
  paragraphs.load("$none");

  await context.sync();

  // Start new list using the second paragraph.
  const list = paragraphs.items[1].startNewList();
  list.load("$none");

  await context.sync();

  // To add new items to the list, use Start or End on the insertLocation parameter.
  list.insertParagraph("New list item at the start of the list", "Start");
  const paragraph = list.insertParagraph("New list item at the end of the list (set to list level 5)", "End");

  // Sets up list level for the list item.
  paragraph.listItem.level = 4;

  // To add paragraphs outside the list, use Before or After.
  list.insertParagraph("New paragraph goes after (not part of the list)", "After");

  await context.sync();
});

My issue is,

Paragraphs are created into word document BUT cursor not adjusted to end range

Is there any APIs OR solution to fix this?

Thanks

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
864 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Eugene Astafiev 891 Reputation points
    2023-01-25T20:28:49.6866667+00:00

    The Office JavaScript API doesn't provide anything for setting a cursor at a specific place. You may play with a selection as a possible workaround by selecting ranges.

    You can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team goes through the planning process.

    0 comments No comments

  2. Eugene Astafiev 891 Reputation points
    2023-01-25T20:29:59.6533333+00:00

    The Office JavaScript API doesn't provide anything for setting a cursor at a specific place. You may play with a selection as a possible workaround by selecting ranges.

    You can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team goes through the planning process.

    0 comments No comments