Xxxaaavvv a écrit :
alors de mémoire
l'objet command bar tu ne doit pas l'ajouter, mais utiliser celui qui est déjà en place.
la tu fais de la programmation VB avancée
bon voici un module que je m'étais refait y a pas longtemps :
Code :
- Option Explicit
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- ' Projet : Outil Portable
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Fichier : modMenus
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Création : 09/02/2008
- ' Auteur : Xxxaaavvv
- ' Version : 1.0
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Description : Gestion des Menus d'Excel
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- ' Procédure : fg_ChargementMenu
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Création : 09/02/2008
- ' Auteur : Xxxaaavvv
- ' Version : 1.0
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Entrée : strCommandBarName -> nom de la Barre contenant les menus
- ' : strMenuName -> nom du menu à ajouter
- ' Sortie : booléen 'True' si succès, 'False' sinon
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Description : Ajoute un menu directement à la barre de commande concernée
- ' : "Worksheet Menu Bar" représente la barre de menu des feuilles Excel (= strCommandBarName)
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- Public Function fg_ChargementMenu(strCommandBarName As String, strMenuName As String) As Boolean
- On Error GoTo fg_ChargementMenu_Error
-
- Dim objMenu As CommandBar
- Dim objPopup As CommandBarPopup
- Dim blnPopupPresent As Boolean
-
- fg_ChargementMenu = False
- 'On parcours les barres de menu
- For Each objMenu In Application.CommandBars
- If objMenu.Name = strCommandBarName Then
-
- 'On teste si le menu racine est déjà présent dans la barre
- For Each objPopup In objMenu.Controls
- If objPopup.Caption = strMenuName Then
- blnPopupPresent = True
- End If
- Next objPopup
-
- 'S'il n'est pas présent on le rajoute
- If Not blnPopupPresent Then
- Set objPopup = objMenu.Controls.Add(msoControlPopup, , , 10, True)
- objPopup.Caption = strMenuName
- End If
-
- End If
- Next objMenu
- Exit Function
- fg_ChargementMenu_Error:
- End Function
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- ' Procédure : fg_ChargementSousMenu
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Création : 09/02/2008
- ' Auteur : Xxxaaavvv
- ' Version : 1.0
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Entrée : strCommandBarName -> nom de la Barre contenant les menus
- ' : strMenuName -> nom du menu impacter
- ' : strSousMenuCaption -> caption du sous-menu à ajouter
- ' : strSousMenuOnAction -> procédure à lancer quand le sous-menu est cliqué
- ' Sortie : booléen 'True' si succès, 'False' sinon
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Description : Ajoute un sous-menu directement dessous le menu de la barre de commande concernée
- ' : Par exemple "Worksheet Menu Bar" représente la barre de menu des feuilles Excel (= strCommandBarName)
- ' : suffit ensuite de prendre n'importe quel menu existant et de lui ajouter un sous-menu...
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- Public Function fg_ChargementSousMenu(strCommandBarName As String, strMenuName As String, strSousMenuCaption As String, strSousMenuOnAction As String) As Boolean
- On Error GoTo fg_ChargementSousMenu_Error
- Dim objMenu As CommandBar
- Dim objPopup As CommandBarPopup
- Dim objButton As CommandBarButton
- fg_ChargementSousMenu = False
- 'On parcours les barres de menu
- For Each objMenu In Application.CommandBars
- If objMenu.Name = strCommandBarName Then
- 'On parcours les menus de la barre
- For Each objPopup In objMenu.Controls
- If objPopup.Caption = strMenuName Then
- 'On ajoute le bouton de sous-menu
- Set objButton = objPopup.Controls.Add(Type:=msoControlButton)
- objButton.Caption = strSousMenuCaption
- objButton.OnAction = strSousMenuOnAction
- fg_ChargementSousMenu = True
- End If
- Next objPopup
- End If
- Next objMenu
- Exit Function
- fg_ChargementSousMenu_Error:
- End Function
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- ' Procédure : fg_DechargementSousMenu
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Création : 09/02/2008
- ' Auteur : Xxxaaavvv
- ' Version : 1.0
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Entrée : strCommandBarName -> nom de la Barre contenant les menus
- ' : strMenuName -> nom du menu impacter
- ' : strSousMenuCaption -> caption du sous-menu à supprimer
- ' Sortie : booléen 'True' si succès, 'False' sinon
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Description : Supprime un sous-menu du menu de la barre de commande concernée
- ' : "Worksheet Menu Bar" représente la barre de menu des feuilles Excel (= strCommandBarName)
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- Public Function fg_DechargementSousMenu(strCommandBarName As String, strMenuName As String, strSousMenuCaption As String) As Boolean
- On Error GoTo fg_DechargementSousMenu_Error
- Dim objMenu As CommandBar
- Dim objPopup As CommandBarPopup
- Dim objButton As CommandBarButton
- fg_DechargementSousMenu = False
- 'On parcours les barres de menu
- For Each objMenu In Application.CommandBars
- If objMenu.Name = strCommandBarName Then
- 'On parcours les menus de la barre
- For Each objPopup In objMenu.Controls
- If objPopup.Caption = strMenuName Then
- 'On parcours les sous-menu
- For Each objButton In objPopup.Controls
- If objButton.Caption = strSousMenuCaption Then
- 'On supprime le bouton de menu
- objButton.Delete
- fg_DechargementSousMenu = True
- End If
- Next objButton
- End If
- Next objPopup
- End If
- Next objMenu
- Exit Function
- fg_DechargementSousMenu_Error:
- End Function
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- ' Procédure : fg_SousMenuPresent
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Création : 09/02/2008
- ' Auteur : Xxxaaavvv
- ' Version : 1.0
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Entrée : strCommandBarName -> nom de la Barre contenant les menus
- ' : strMenuName -> nom du menu impacter
- ' : strSousMenuCaption -> caption du sous-menu à tester
- ' Sortie : booléen 'True' si succès, 'False' sinon
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ' Description : Teste la présence d'un sous menu dans excel
- ' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- Public Function fg_SousMenuPresent(strCommandBarName As String, strMenuName As String, strSousMenuCaption As String) As Boolean
- On Error GoTo fg_SousMenuPresent_Error
- Dim objMenu As CommandBar
- Dim objPopup As CommandBarPopup
- Dim objButton As CommandBarButton
- fg_SousMenuPresent = False
- 'On parcours les barres de menu
- For Each objMenu In Application.CommandBars
- If objMenu.Name = strCommandBarName Then
- 'On parcours les menus de la barre
- For Each objPopup In objMenu.Controls
- If objPopup.Caption = strMenuName Then
- 'On parcours les sous-menu
- For Each objButton In objPopup.Controls
- If objButton.Caption = strSousMenuCaption Then
- 'Le menu est présent
- fg_SousMenuPresent = True
- End If
- Next objButton
- End If
- Next objPopup
- End If
- Next objMenu
- Exit Function
- fg_SousMenuPresent_Error:
- End Function
|
et voici ce que j'avais dans mon workbook_open :
Code :
- Private Sub Workbook_Open()
- On Error GoTo Workbook_Open_Error
-
- Const strCommandBarName As String = "Worksheet Menu Bar"
- Const strMenuPNCaption As String = "Outils &PN"
-
- Call fg_ChargementMenu(strCommandBarName, strMenuPNCaption)
- Call fg_ChargementSousMenu(strCommandBarName, strMenuPNCaption, "&Supprimer les lignes barrées", "modOutilsVBA.subMenuSuppressionLignes" )
- 'Call fg_ChargementSousMenu(strCommandBarName, strMenuPNCaption, "&Options", "modMain.subMenuOptions" )
- 'Call fg_ChargementSousMenu(strCommandBarName, strMenuPNCaption, "&Test", "modOutilsVBA.subMenuTest" )
-
- Exit Sub
- Workbook_Open_Error:
- End Sub
|
je te laisse adapter ça tout seul comme un grand :D
|