VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel)

时间:2023-02-13 08:12:55

原文:VSTO 为Office已有右键菜单添加自己的菜单项(word,Excel)

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

对于Outlook和PPT具体还不清楚,正在查证中。。。