views:

942

answers:

1

I am trying to use the FindControl Method of the CommandBars object in a VSTO Word addin to get what else a command bar object Code is as follows

 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)
        {
        }
    }

The FindControl method is throwing a Type Mismatch Exception (-2147352571) Any ideas is this the right way anyhow to add a item to the right click menu of word and then make sure you dont add it if it already exists Thanks

+1  A: 

you are using Missing where Missing is not allowed as parameter ref: link text http://msdn.microsoft.com/en-us/library/system.type.missing.aspx

use code like this:

        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);
Fei