1

我需要在运行时使用 C# 在 Excel 命令栏中添加一个组标题,以便我可以将我的 Office.CommandBarButton 选项与默认的 Excel 选项分开。例如,在 Excel 2013 中,如果您选择一行和 RMB(鼠标右键),默认的命令栏将显示许多选项。您会注意到有一个名为“粘贴选项:”的标题,左侧带有标准粘贴图标。我想使用 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 RMB 选项分开。

4

1 回答 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);
}
于 2019-02-05T06:27:39.633 回答