MS Word Office.js API to add new content control along with text "before" target content control in WEB version #182627
Replies: 2 comments
-
|
Hey! I've run into this exact behavior before. The inconsistency between Desktop and Web when handling ranges at the boundaries of Content Controls is super annoying. The issue stems from how you are defining the insertion point. When you grab getRange(Word.RangeLocation.before) and then insert text Word.InsertLocation.after that range, the Web engine interprets that "sticky" boundary as being inside the nearest container (your target control). To fix this, you should avoid manipulating the Range and instead insert the paragraph directly on the Content Control object. This tells the engine to create a structural sibling node, rather than guessing where the text belongs. Try this approach: The key difference here is targetControl.insertParagraph(..., Word.InsertLocation.before). Also, don't skip the intermediate context.sync()—the Web version is much stricter about objects needing to be fully instantiated before you can wrap them in a new CC. |
Beta Was this translation helpful? Give feedback.
-
|
Okay, so the problem you're running into with Word Online is a common one. It's all about how the web version handles those Range Boundary things. When you use To dodge this issue and keep things separate on both the desktop and web versions, skip making a range yourself. Just have the Here's the easiest way to do it with TypeScript: // Find the control you're aiming for (like you're already doing)
const targetControl = controls.items.find((c: any) => {
return (c.tag || ).trim().startsWith(targetTag);
});
if (targetControl && position === before) {
// 1. Shove the paragraph in directly BEFORE the control.
// This makes sure the new paragraph is outside the target's area.
const newParagraph = targetControl.insertParagraph(<some text="">, Word.InsertLocation.before);
// 2. Wrap the</some> new paragraph in its own content control.
const newCC = newParagraph.insertContentControl();
// If you want, set some details for the new control
newCC.tag = new-control-tag;
await context.sync();
}
Give this a shot – it's way more reliable across the board than messing with those |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Hi !
I am developing MS Word Add-in using typescript where-
Word document will have "target content control" and need to add new content control "before" the target.
I followed the typescript code like below (not the exact code, tried multiple trials)-
Issue I am seeing is-
newly added content control along with text is added at the beginning of the target content control.
When I hover on those, it shows the boundary like this-
Expectation is to have two separate content controls.
It works in Desktop but not in Web version.
Is this case feasible in Word web version? If Yes, please suggest sample typescript code.
Thanks!
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions