Home » c# » C# Word Interop VSTO inserting content control with Xml content modifies formating of paragraph where it is inserted

C# Word Interop VSTO inserting content control with Xml content modifies formating of paragraph where it is inserted

Posted by: admin February 21, 2020 Leave a comment

Questions:

As the title suggests i’m trying to insert a content control with XML to the end of the range of a paragraph.

The problem is when i insert it into the range and there is content already there for example some text that is formatted that text gets reformatted to the content controls format.

Range headerEndRange = header.Range.Duplicate;

var lastParagraphRange = headerEndRange.Paragraphs.Last.Range;
lastParagraphRange.Collapse(WdCollapseDirection.wdCollapseEnd);

Microsoft.Office.Tools.Word.RichTextContentControl control = vstoDocument.Controls.AddRichTextContentControl(lastParagraphRange, GetNewDraftControlName());
control.Tag = DRAFT_CONTROL_TAG;

DocumentUtils.InsertXMLWithoutExtraParagraph(control.InnerObject, template);

Example header content:

Header text 1

Header text 2

Both of them are paragraphs which are inserted below each other. It can be only one paragraph also. The count does not matter. The content control gets inserted to the end range of the last paragraph in the header in the above example Header text 2.

The paragraph (Header text 2) can have custom formatting meaning a part of the text can be 12px bold Arial and another part can be 15px italic Times New Roman.

When the content control gets inserted with the xml the paragraph looses its formatting it gets left aligned the font size is unset and so on.

I hope this helps explain the situation better.

Any help would be appreciated!

How to&Answers: