0

我正在尝试在 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 的右键菜单,然后确保如果它已经存在则不要添加它谢谢

4

1 回答 1

1

您正在使用 Missing where Missing is not allowed 作为参数参考:链接文本 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);
于 2009-05-15T06:51:42.803 回答