VSTO Excel [英] VSTO Excel

查看:59
本文介绍了VSTO Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在工作表中右键单击时,如何添加在Excel文档的上下文菜单中显示的条目(即菜单项)?


 我是使用Excel 2007& VSTO 4.0

解决方案

您好,


试试这段代码..希望这有助于您!


 
public partial class ThisWorkbook
{
Microsoft.Office.Core.CommandBarPopup shortcutMenu;
Microsoft.Office.Core.CommandBarButton menuItemER;

private void ThisWorkbook_Startup( object sender,System.EventArgs e)
{


Excel.Application ExApp = Globals.ThisWorkbook.Application as Excel.Application;
shortcutMenu =(Microsoft.Office.Core.CommandBarPopup)ExApp.CommandBars [" Cell" ]。Controls.Add(MsoControlType.msoControlPopup,System .Type.Missing,System.Type.Missing,System.Type.Missing, true );
shortcutMenu.Caption = "示例菜单";
menuItemER =(Microsoft.Office.Core.CommandBarButton)shortcutMenu.Controls.Add(MsoControlType.msoControlButton,System.Type.Missing,System.Type.Missing,System.Type.Missing,);
menuItemER.Caption = "子菜单项1";
menuItemER.BeginGroup = true ;
menuItemER.Click + = new _CommandBarButtonEvents_ClickEventHandler(menuItemER_Click);

}

void menuItemER_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
MessageBox.Show(" Sub单击菜单项");
}

private void ThisWorkbook_Shutdown( object sender,System.EventArgs e)
{
}

#区域 VSTO Designer生成的代码

/// < summary>
/// Designer支持所需的方法 - 不要修改
/// 使用代码编辑器打开此方法的内容。
/// < / summary>
私有 void InternalStartup()
{
this .Startup + = new System.EventHandler(This Workbook_Startup);
这个 .Shutdown + = new System.EventHandler(ThisWorkbook_Shutdown);
}

#endregion

}


how can i add an entry(i.e. menu item ) which gets displayed in the contextual menu of Excel Doc when right clicked in the worksheets?

 I am using Excel 2007 & VSTO 4.0

解决方案

Hi,

Try this piece of code.. Hope this helps you!

  public partial class ThisWorkbook
  {
    Microsoft.Office.Core.CommandBarPopup shortcutMenu;
    Microsoft.Office.Core.CommandBarButton menuItemER;

    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {


      Excel.Application ExApp = Globals.ThisWorkbook.Application as Excel.Application;
      shortcutMenu = (Microsoft.Office.Core.CommandBarPopup)ExApp.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
      shortcutMenu.Caption = "Sample Menu";
      menuItemER = (Microsoft.Office.Core.CommandBarButton)shortcutMenu.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
      menuItemER.Caption = "Sub Menu Item 1";
      menuItemER.BeginGroup = true;
      menuItemER.Click += new _CommandBarButtonEvents_ClickEventHandler(menuItemER_Click);
 
    }

    void menuItemER_Click(CommandBarButton Ctrl, ref bool CancelDefault)
    {
      MessageBox.Show("Sub Menu item clicked");
    }

    private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
      this.Startup += new System.EventHandler(ThisWorkbook_Startup);
      this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
    }

    #endregion

  }


这篇关于VSTO Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆