【问题标题】:VSTO Word Add-In : Content Control NestingVSTO Word 加载项:内容控制嵌套
【发布时间】:2012-04-16 06:46:12
【问题描述】:

我正在为我正在实习的公司构建一个 MS-Word 插件。

我已经创建了一个带有很多 SplitButtonsButtons 的新功能区。 现在我想做的是,当您单击其中一个按钮时,内容控件将添加到 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


    【解决方案1】:

    而不是

    contentControlPlain = doc.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref oRng);
    

    使用

    contentControlPlain = richTextControl.Range.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref oRng);
    

    在使用上面的代码之前使用下面的代码

    Application.Selection.Start = lastControlinRichTextControl.Range.End+1;
    

    并设置 `oRng = Application.Selection.Range

    【讨论】:

      【解决方案2】:

      根据消息,您的代码正在尝试在富文本控件(即现有的纯文本控件)中的所有内容周围包装一个纯文本控件。修复您的范围对象,使其不会这样做,例如将其折叠到富文本控件内的某个点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多