Insert content control not working properly in paragraph

Alpesh Chaudhari 0 Reputation points
2023-09-22T07:27:16.7833333+00:00

Hello Team,

I am using Word API for developing word add-in.

My Environment

Platform : PC desktop

Host: Word

Office version number: Microsoft® Word for Microsoft 365 MSO (Version 2308 Build 16.0.16731.20182) 64-bit

Operating System: Windows 10 pro

Snippet :

 Word.run(async (context) => {
    var range = context.document.getSelection();
    range.load("text");
    await context.sync();
    var ctrl = range.insertContentControl();
    ctrl.tag ="tempId";    
    ctrl.load();
    await context.sync();
});

When using the provided code snippet to insert content controls into a document, it appears to work correctly in most cases. However, there is an issue with certain paragraphs where selecting the entire paragraph and inserting a content control results in the control being applied only to a specific line within the paragraph, rather than the entire paragraph. How can i resolve this issue ?

Screen short for selected paragraph range:

Paragraph sample 1:

ParagraphSample1

Paragraph sample 2:

ParagraphSample2

Paragraph sample 3:

ParagraphSample3

Word document sample:

document.xml

Note :please change above document extension xml to .docx for word document.

To Reproduce steps:

  • Select specified the paragraphs
  • Insert content control with above snippet

Could you please provide me with recommendations or solutions for this matter at your earliest convenience?
Thank you...

Microsoft 365 and Office | Development | Office JavaScript API
Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Word | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Elizabeth Samuel MSFT 11 Reputation points Microsoft Employee
    2023-09-25T23:13:58.8766667+00:00

    @Alpesh Chaudhari The API crew advises that this behavior is by design, not only by the API but also in the Word UI. The first paragraph in the list item is captured in the content control.

    They suggest that, instead of a line break (when you press Enter to create a new paragraph), you consider removing it or using a soft line break (press Shift+Enter to keep the content before and after the line break in the same paragraph).

    Hope this helps.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.