WPFKK
WPFKK

Reputation: 1477

Adding content control throws an exception dynamically

I am fairly new to Word Addin development. Fortunately I was able to do almost everything but stuck at some simple issue I belive.

I want to insert plain text controls dynamically at the selected range. For this I am using the following:

    currentDocument = application.ActiveDocument;
    foreach(var field in myFieldsList)
    {      
         Microsoft.Office.Interop.Word.Range rng = currentDocument.ActiveWindow.Selection.Range;
         object oRng = rng;
         var contentControlPlain = application.ActiveDocument.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref oRng);
         contentControlPlain.Tag = formField.FormFieldId.ToString();
         contentControlPlain.SetPlaceholderText(null, null, " <" + formField.FormFieldName + "> ");
         contentControlPlain.LockContentControl = (formField.TypeName.Trim() == "Blank");
    }

Code seems to be working fine but when I try to insert the second field it complains saying:

This method or property is not available because the current selection partially covers a plain text content control.

I understand that addin is trying to insert next content control into the previously inserted plain text control. But I tried giving some other range and could not fix it. Any help is greatly appreciated.

Thanks.

Upvotes: 1

Views: 1670

Answers (1)

After adding every content control use

Application.Selection.Start = lastControl.Range.End+1

Upvotes: 1

Related Questions