【发布时间】:2012-04-16 06:46:12
【问题描述】:
我正在为我正在实习的公司构建一个 MS-Word 插件。
我已经创建了一个带有很多 SplitButtons 和 Buttons 的新功能区。
现在我想做的是,当您单击其中一个按钮时,内容控件将添加到 word doc 中。
这适用于普通内容控件。这些内容控件具有诸如“运动/篮球/球员/姓名”之类的标签,这些标签绑定到 XML 文件中的元素。
private void addSimpleContentControl(String tag, String placeholder)
{
try
{
contentControlPlain = Globals.ThisAddIn.Application.ActiveDocument.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText);
contentControlPlain.Tag = tag;
contentControlPlain.SetPlaceholderText(null, null, placeholder);
}
catch (COMException) { }
}
现在让我们谈谈我的问题。 我的一些元素可能会出现不止一次。所以我想要创建的是一个富内容控件,它包含多个纯内容控件。
所以我有一个SplitButton“球员”,上面有“姓名”、“球衣号码”、“位置”等按钮......
单击其中一个基础按钮时,我首先检查是否已存在具有特定名称的富文本控件。
如果不是,我制作一个并向其添加一个纯内容控件。
富内容控件->纯文本控件->富内容控件结束
到目前为止一切顺利,一切都很好,但从我想向丰富内容控件添加另一个普通内容控件的那一刻起,这会弹出:
“不能在其他控件或 XML 元素周围插入纯文本控件”
这是我将纯内容控件添加到富内容控件的代码。
private void addContentControlToRich(String tag, String placeholder,String title)
{
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
foreach (Microsoft.Office.Interop.Word.ContentControl cc in doc.ContentControls)
{
if (cc.Title == title && cc.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
{
try
{
Microsoft.Office.Interop.Word.Range rng = cc.Range;
object oRng = rng;
contentControlPlain = doc.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref oRng);
contentControlPlain.Tag = tag;
contentControlPlain.SetPlaceholderText(null, null, placeholder);
contentControlPlain.LockContentControl = true;
break;
}
catch (COMException) { }
}
}
}
【问题讨论】:
标签: c# ms-word vsto ribbonx word-contentcontrol