IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VSTO .NET Discussion :

Ajout d'un bouton dans complément de EXCEL


Sujet :

VSTO .NET

  1. #1
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Février 2009
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Février 2009
    Messages : 20
    Points : 10
    Points
    10
    Par défaut Ajout d'un bouton dans complément de EXCEL
    Bonjour,
    Je souhaiterais savoir s'il existe un moyen en c# dans un addins d'ajouter un bouton dans les compléments EXCEL pour déclencher un traitement.
    En fait, je ne sais pas dans quelle classe de Application je dois le faire ni la classe du bouton.
    Tout exemple serait la bienvenue.
    Merci d'avance.
    Tf

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut menu et barre d'outils dans un add-in vsto
    bonjour
    voic un exemple de menu et un exemple de barre d'outils dans un add-in excel 2003 (ou word ou powerpoint ......).
    exemple 1:rajout d'une barre de menu dans un projet complement excel 2003 (add-in excel)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
        }
    }
    exemple 2:rajout d'une barre d'outils dans un projet complement word 2003 (add-in wordl)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    //EXEMPLE AJOUT D'UNE BARRE D'OUTILS WORD 2003
    using System;
    using System.Windows.Forms;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;
     
    namespace WordAddIn1
    {
        public partial class ThisAddIn
        {
            // Create the command bar variables at the class level.
            Office.CommandBar commandBar;
            Office.CommandBarButton firstButton;
            Office.CommandBarButton secondButton;
     
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                AddToolbar();
            }
     
            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
     
     
     
            private void AddToolbar()
            {
                try
                {
                    commandBar = Application.CommandBars["Test"];
                }
                catch (ArgumentException e)
                {
                    // Toolbar named Test does not exist so we should create it.
                }
     
                if (commandBar == null)
                {
                    // Add a commandbar named Test.
                    commandBar = Application.CommandBars.Add("Test", 1, missing, true);
                }
     
                try
                {
                    // Add a button to the command bar and an event handler.
                    firstButton = (Office.CommandBarButton)commandBar.Controls.Add(
                        1, missing, missing, missing, missing);
     
                    firstButton.Style = Office.MsoButtonStyle.msoButtonCaption;
                    firstButton.Caption = "button 1";
                    firstButton.Tag = "button1";
                    firstButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);
     
                    // Add a second button to the command bar and an event handler.
                    secondButton = (Office.CommandBarButton)commandBar.Controls.Add(
                        1, missing, missing, missing, missing);
     
                    secondButton.Style = Office.MsoButtonStyle.msoButtonCaption;
                    secondButton.Caption = "button 2";
                    secondButton.Tag = "button2";
                    secondButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);
     
                    commandBar.Visible = true;
                }
                catch (ArgumentException e)
                {
                    MessageBox.Show(e.Message);
                }
            }
     
            // Handles the event when a button on the new toolbar is clicked.
            private void ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
            {
                MessageBox.Show("You clicked: " + ctrl.Caption);
            }
     
        }
    }
    BON CODE....

  3. #3
    Membre à l'essai
    Homme Profil pro
    Inscrit en
    Février 2009
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Février 2009
    Messages : 20
    Points : 10
    Points
    10
    Par défaut
    Merci beaucoup pour votre réponse.
    J'ai essayé et celà marche.
    tf

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. insérer un bouton dans une feuille excel lié à une fonction vba
    Par thierry_b dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 16/06/2009, 12h14
  2. Ajout d'un bouton dans une feuille excel avec vba
    Par nsqualli dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 25/11/2008, 15h35
  3. [JTable] ajout d'un bouton dans une colonne
    Par Blast dans le forum Composants
    Réponses: 7
    Dernier message: 15/03/2007, 17h33
  4. Insérer automatiquement des boutons dans des cellules excel
    Par Oaurelius dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 04/11/2005, 18h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo