VSTO添加右键菜单

以Word为例

 private void AddRightMenu()
        {
            Microsoft.Office.Core.CommandBar mzBar = appWord.CommandBars["Text"];    //word文档已有的右键菜单Text
            Microsoft.Office.Core.CommandBar mzBar = appExcel.CommandBars["cell"];    //excel文档已有的右键菜单cell
            Microsoft.Office.Core.CommandBarControls bars = mzBar.Controls;
            foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars)
            {
                string t = temp_contrl.Tag;
                //如果已经存在就删除
                if (t == "Test")
                {
                    temp_contrl.Delete();
                }
            }
            Microsoft.Office.Core.CommandBarControl comControl = bars.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true);   //添加自己的菜单项
            Microsoft.Office.Core.CommandBarButton comButton = comControl as Microsoft.Office.Core.CommandBarButton;
            if (comControl != null)
            {
                comButton.Tag = "Test";
                comButton.Caption = "测试";
                comButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
                comButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);
            }
        }

 

posted @ 2023-05-29 11:05  六镇2012  阅读(258)  评论(0编辑  收藏  举报