发布网友 发布时间:2022-04-25 16:36
共2个回答
热心网友 时间:2023-10-18 15:25
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Excel.Application appExcel = Globals.ThisAddIn.Application;
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 = "Test1";
comButton.Caption = "公式转数值";
comButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
comButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);
}
Microsoft.Office.Core.CommandBarControl comControl1 = bars.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true); //添加自己的菜单项
comControl.Move(missing,1);
comControl1.Move(missing, 2);
Microsoft.Office.Core.CommandBarButton comButton1 = comControl1 as Microsoft.Office.Core.CommandBarButton;
if (comControl1 != null)
{
comButton1.Tag = "Test2";
comButton1.Caption = "区域互换";
comButton1.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
comButton1.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);
}
}
private void menuCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
Tools Mytools = new Tools();
switch (Ctrl.Tag)
{
case "Test1"://
Mytools.DelFormula();自己写的功能宏
break;
case "Test2"://
Mytools.ChangeRegion();自己写的功能宏
break;
}
热心网友 时间:2023-10-18 15:26
newBtn.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
这句不要估计就行了