1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
//EXEMPLE AJOUT D'UN MENU DANS BARRE DE MENU STANDARD EXCEL 2003
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
namespace ExcelAddIn1
{
public partial class ThisAddIn
{
// Declare a workbook and Worksheet
Excel.Workbook myWorkbook;
Excel.Worksheet myWorksheet;
// Declare the menu variable at the class level.
private Office.CommandBarButton menuCommand;
private string menuTag = "A unique tag";
// Call AddMenu from the Startup event of ThisWorkbook.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
#region Code généré par VSTO
this.Application = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), this.Application);
//Code menu
CheckIfMenuBarExists();
AddMenuBar();
myWorkbook = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
myWorksheet = (Excel.Worksheet)myWorkbook.Worksheets[1];
#endregion
}
// If the menu already exists, remove it.
private void CheckIfMenuBarExists()
{
try
{
Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
this.Application.CommandBars.ActiveMenuBar.FindControl(
Office.MsoControlType.msoControlPopup, System.Type.Missing, menuTag, true, true);
if (foundMenu != null)
{
foundMenu.Delete(true);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// Create the menu, if it does not exist.
private void AddMenuBar()
{
try
{
Office.CommandBarPopup cmdBarControl = null;
Office.CommandBar menubar = (Office.CommandBar)Application.CommandBars.ActiveMenuBar;
int controlCount = menubar.Controls.Count;
string menuCaption = "&New Menu";
// Add the menu.
cmdBarControl = (Office.CommandBarPopup)menubar.Controls.Add(
Office.MsoControlType.msoControlPopup, missing, missing, controlCount, true);
if (cmdBarControl != null)
{
cmdBarControl.Caption = menuCaption;
// Add the menu command.
menuCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(
Office.MsoControlType.msoControlButton, missing, missing, missing, true);
menuCommand.Caption = "&New Menu Command";
menuCommand.Tag = "NewMenuCommand";
menuCommand.FaceId = 65;
menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
menuCommand_Click);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
// Add text to cell A1 when the menu is clicked.
private void menuCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
myWorksheet.get_Range( "A1", missing).Value2 = "The menu command was clicked.";
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region Code généré par VSTO
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
} |
Partager