Adding content control throws an exception dynamically

六眼飞鱼酱① 提交于 2019-12-11 08:30:36

问题


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.


回答1:


After adding every content control use

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


来源:https://stackoverflow.com/questions/11024481/adding-content-control-throws-an-exception-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!