Insert Text at the Cursor in a Word JavaScript Add-in
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.
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.
Insert inline text with the Word JavaScript API
Use the current selection and insertText to place text inline instead of creating a paragraph.
In your add-in's insertion handler, use Word.run and call context.document.getSelection() to obtain the current document selection.
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(); });
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.
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.

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.




