Document Editing Problems

Insert Text at the Cursor in a Word JavaScript Add-in

Emma BrownEmma Brown Nov 14, 2026 868 views

Question details

Insert text at the user's current cursor position or selection from a Word JavaScript add-in.

Product
Microsoft Word JavaScript API
Device & OS
not provided
Scenario
A developer wants an add-in to insert a value inline at the current Word document selection.
Observed behavior
The add-in inserts text in a new paragraph or at a default alignment position instead of inline at the cursor.
Before you start

Open the document and place the cursor where the text should appear, or select text you want to replace. Make sure your add-in is using the Word JavaScript API and has access to the document.

Solution 1Recommended

Insert inline text with the Word JavaScript API

Use the current selection and insertText to place text inline instead of creating a paragraph.

1
Get the current selection

In your add-in's insertion handler, use Word.run and call context.document.getSelection() to obtain the current document selection.

2
Insert the value

Call insertText on the selection with Word.InsertLocation.replace. For example: await Word.run(async (context) => { const selection = context.document.getSelection(); selection.insertText('{{' + this.state.value + '}}', Word.InsertLocation.replace); await context.sync(); });

3
Test at the cursor

Place the cursor inside a paragraph in Word, then activate the add-in's insertion button. Confirm that the value appears inline at that position.

Selected text is replaced: Word.InsertLocation.replace replaces any selected text. To preserve it and insert before it, use Word.InsertLocation.before instead. Use insertText rather than insertParagraph when you do not want a new paragraph.
Free Microsoft Office alternative

Edit Word Documents with WPS Writer

WPS Office is a free, lightweight alternative for editing documents in a familiar interface. Open Word files and continue working with compatible formats for an easy transition. It does not fix Microsoft Word JavaScript add-in behavior.

Insert and edit text at the cursor in WPS WriterOpen and save Microsoft Word DOC and DOCX filesUse familiar document-editing controlsWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does my add-in put the text on a new line?

Calling insertParagraph creates a paragraph. For inline text, call context.document.getSelection().insertText() instead.

Will insertText replace the user's selection?

Yes, when you use Word.InsertLocation.replace. To insert text before the selection without replacing it, use Word.InsertLocation.before.

Why do I need to call context.sync()?

The Word JavaScript API queues commands inside Word.run. Call await context.sync() after insertText to send the queued insertion to Word.

Can I use Selection.InsertBefore in a Word JavaScript add-in?

Selection.InsertBefore belongs to the Word object model, not the Word JavaScript API. In a JavaScript add-in, use getSelection().insertText() with Word.InsertLocation.before instead.