CommandBars.FindControl抛出异常

时间:2008-12-29 05:54:21

标签: ms-word vsto add-in

我正在尝试在VSTO Word插件中使用CommandBars对象的FindControl方法来获取其他命令栏对象 代码如下

 private void WireContextMenu(string MenuID,string Tag, string ID, ref Office.CommandBarButton Control)
    {
        try
        {
            object missing = System.Type.Missing;

            Control = (Office.CommandBarButton)this.Application.CommandBars[MenuID].FindControl((object)Office.MsoControlType.msoControlButton, ID, Tag, missing, missing);
            if (Control == null)
            {
                Control = (Office.CommandBarButton)this.Application.CommandBars[MenuID].Controls.Add(Office.MsoControlType.msoControlButton, ID, missing, missing, missing);
                Control.Caption = "Biolit Markup Selection";
                Control.Tag = Tag;
            }

            Control.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.cb_Click);
        }
        catch (Exception Ex)
        {
        }
    }

FindControl方法抛出类型不匹配异常(-2147352571) 有任何想法吗 这是正确的方式,无论如何将项目添加到word的右键菜单,然后确保你不添加它,如果它已经存在 谢谢

1 个答案:

答案 0 :(得分:1)

您正在使用Missing,其中Missing不允许作为参数 参考:link text http://msdn.microsoft.com/en-us/library/system.type.missing.aspx

使用这样的代码:

        object type = MsoControlType.msoControlPopup;
        object id = 1;
        object tag = null;
        object visible = 1;
        object recusive = false;
        //object missing = System.Type.Missing;

        CommandBarControl barControl = popParent.FindControl(type, id, tag, visible, recusive);