/// <summary> /// 删除并添加右键菜单 /// </summary> private void RemoveAndAddRightBtns(bool isEnable = false) { try { string nameMenue = "SearchSynonym"; Microsoft.Office.Core.CommandBar mzBar = Application.CommandBars["Text"];//word文档已有的右键菜单Text Microsoft.Office.Core.CommandBarControls bars = mzBar.Controls; foreach (Microsoft.Office.Core.CommandBarControl temp_contrl in bars) { string t = temp_contrl.Tag; //如果已经存在就删除 if (t == nameMenue) { temp_contrl.Delete(true); } } object missing = Type.Missing; Microsoft.Office.Core.CommandBarButton addBtn = (Microsoft.Office.Core.CommandBarButton)Application.CommandBars["Text"].Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, false); // 开始一个新Group,即在我们添加的Menu前加一条分割线 addBtn.BeginGroup = true; // 为按钮设置Tag addBtn.Tag = nameMenue; // 添加按钮上的文字 addBtn.Caption = "查找替换词"; // 将按钮初始设为不激活状态 addBtn.Enabled = isEnable; addBtn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(_FindRepalceWordBtn_Click); } catch (Exception ex) { } }
private void AddIn_Startup(object sender, System.EventArgs e) { Microsoft.Office.Core.CommandBar cmdBar = Application.CommandBars["cell"]; Microsoft.Office.Core.CommandBarControl comCtrl = cmdBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true); Microsoft.Office.Core.CommandBarButton comButton = comCtrl as Microsoft.Office.Core.CommandBarButton; comButton.Tag = "Validate"; comButton.Caption = "校验列"; comButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIcon; comButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btnCalc_Click); }