如何使用C#Excel中添加一行命令栏标题的文字和图标

问题描述 投票:1回答:1

我需要用C#在运行时在Excel中命令栏添加组头,这样我就可以独立于默认Excel选项我Office.CommandBarButton选项。例如,在Excel 2013中,如果你去选择一个行人民币(右鼠标键),默认命令栏将有许多选项中显示。你会发现,有一个叫头“粘贴选项:”与标准粘贴图标的左边。我想创建一个类似头(组),如“粘贴选项:”使用C#。

顺便说一句,我用下面的代码示例在Excel中成功添加几个Office.CommandBarButton选择;

    private void AddMyRowMenu() 
    {
       Office.CommandBars commandBars = null;
       Office.CommandBar commandBarRowMenu = null;
       Office.CommandBarButton commandBarButtonMyOptions1;
       try
       {
           commandBarRowMenu = commandBars["Row"];
           commandBarButtonMyOptions1 = (Office.CommandBarButton)commandBarRowMenu.Controls["My Option 1"];
       }
       catch (ArgumentException)
       {
           commandBarButtonMyOptions1 = (Office.CommandBarButton)commandBarRowMenu.Controls.Add(Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, oMissing);
           commandBarButtonMyOptions1.BeginGroup = true;
           commandBarButtonMyOptions1.Caption = "My Option 1";
       }
       commandBarButtonMyOptions1.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(commandBarButtonMyOptions1_Click);
    }

我添加使用上面的代码3个Office.CommandBarButton选项,并需要将它们从默认的Excel人民币选项清晰分开。

c# excel vsto commandbar
1个回答
0
投票

据微软称,只有有限的无控制类型可供我们与https://docs.microsoft.com/en-us/office/vba/api/office.msocontroltype下面的工作就是用来实现上下文菜单我的示例代码;

private void AddMyRowMenu2() 
{
   Office.CommandBars commandBars = null;
   Office.CommandBar commandBarRowMenu = null;
   Office.CommandBarPopup commandBarRowPopupMenu = null;
   Office.CommandBarButton commandBarButtonMyOptions1 = null;
   Office.CommandBarButton commandBarButtonMyOptions2 = null;
   try
   {
       commandBars = (Office.CommandBars)Application.GetType().InvokeMember("CommandBars", System.Reflection.BindingFlags.GetProperty, null, Application, new object[] { });
       commandBarRowMenu = commandBars["Row"];
       commandBarRowPopupMenu = (Office.CommandBarPopup)commandBarRowMenu.Controls.Add(Office.MsoControlType.msoControlPopup, oMissing, oMissing, oMissing, oMissing);
       commandBarRowPopupMenu.Caption = "My Popup Menu 1";
       commandBarButtonMyOptions1 = (Office.CommandBarButton) commandBarRowPopupMenu.Controls.Add(Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, oMissing);
       commandBarButtonMyOptions1.Caption = "My Button Option 1";
       commandBarButtonMyOptions2 = (Office.CommandBarButton) commandBarRowPopupMenu.Controls.Add(Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, oMissing);
       commandBarButtonMyOptions2.Caption = "My Button Option 2";
   }
   catch (ArgumentException ex)
   {
       MessageBox.Show(ex.Message, "Test Menu Items", MessageBoxButtons.OK, MessageBoxIcon.Warning);
   }
   commandBarButtonMyOptions1.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(commandBarButtonMyOptions1_Click);
   commandBarButtonMyOptions2.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(commandBarButtonMyOptions2_Click);
}
© www.soinside.com 2019 - 2024. All rights reserved.