添加内容控件会动态引发异常 [英] Adding content control throws an exception dynamically
问题描述
我对Word Addin开发还很陌生.幸运的是,我几乎可以做所有事情,但是遇到了我相信的一个简单问题.
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:
此方法或属性不可用,因为当前选择部分覆盖了纯文本内容控件.
我知道插件正在尝试将下一个内容控件插入到先前插入的纯文本控件中.但是我尝试给出其他范围,但无法解决. 任何帮助,我们将不胜感激.
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.
谢谢.
推荐答案
添加每个内容控件后
Application.Selection.Start = lastControl.Range.End+1
这篇关于添加内容控件会动态引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!