Hi,
I want to add content controls around existing text within my word document. I have bookmarks around the text to get the range of the text around which I want to add a bookmark.
Below is a code snippet I tried and somewhat helped me achieve the desired output.
private void createContentControlAroundBookmarks(Word._Application WordApp)
{
Word.Document WordDoc = WordApp.ActiveDocument;
Word.Bookmarks allBookMarks = null;
Word.Range tempRange = null;
try
{
allBookMarks = WordDoc.Bookmarks;
allBookMarks.ShowHidden = true;
if (allBookMarks != null)
{
clauseIds = new List<string>();
foreach (Word.Bookmark bookMark in allBookMarks)
{
if (bookMark.Name.EndsWith("_B"))
{
tempRange = bookMark.Range;
tempRange.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText);
}
}
}
}
finally
{
CommonUtils.releaseComObject(allBookMarks);
CommonUtils.releaseComObject(tempRange);
}
}
This works fine in most cases except for a case where range contains ‘\r’.
If my bookmark range constitutes text like “Sentence1. \v\v\r \v Sentence2”. In this case, my content control is added only around “Sentence1” but “Sentence2” is out of my content control.
How can I add content controls properly around my existing text if it contains ‘\r’?
Thanks,