Results 1 to 3 of 3

Thread: [FAQ's: OD] How do I add a custom menu item or toolbar button?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I add a custom menu item or toolbar button?

    Menus and Toolbars are both actually the same objects so to speak. They both are part of the Office CommandBars collection. What specifies them as different between each other is the CommandBar Type.

    A Menu Item is added to the "Menu Bar" CommandBar Item. Conversely, a ToolBar Button is added to the "Standard" CommandBar because the "Standard" CommandBar is the name of the default ToolBar.


    Word 2003 Menu Item Example...



    Word 2003 VBA Code Example:

    VB Code:
    1. Option Explicit
    2.  
    3. '<CREATE THE EVENT HANDLER>
    4. Public WithEvents oCBBCustom As Office.CommandBarButton
    5.  
    6. '<EVENT PROCEDURE>
    7. Private Sub oCBBCustom_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    8.     MsgBox "Meow!"
    9. End Sub
    10.  
    11. Private Sub Document_Open()
    12.     Dim oCB As Office.CommandBar
    13.     Dim oCBBTools As Office.CommandBarPopup
    14.     '<DRILL DOWN THE COMMANDBAR OBJECT HEIRARCHY>
    15.     Set oCB = Application.CommandBars("Menu Bar")
    16.     Set oCBBTools = oCB.Controls("&Tools")
    17.     '<ADD A NEW BUTTON>
    18.     Set oCBBCustom = oCBBTools.Controls.Add(msoControlButton, 1, , , True) 'Place our custom one at the bottom
    19.     With oCBBCustom                                                        'position by leaving it empty or
    20.         .Caption = "VB/Office Guru SpellChecker™"                          'specify a position. (0 based)
    21.         .BeginGroup = True
    22.         .Enabled = True
    23.         .Visible = True
    24.     End With
    25.     '<USE THE .RESET METHOD TO RESET THE COMMAND BAR TO ORIGINAL SETTINGS>
    26.     'oCB.Reset 'To reset the menu.
    27. End Sub
    Last edited by RobDog888; Apr 1st, 2007 at 05:12 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I add a custom menu item or toolbar button?

    Here is another example using different logic.

    Outlook 2003 Menu Item Example...



    Outlook 2003 VBA Code Example:

    VB Code:
    1. Option Explicit
    2. 'Behind ThisOutlookSession
    3. Public WithEvents oMnuSaveAs As Office.CommandBarButton
    4.  
    5. Private Sub SyncMnuSaveAsButton(btn As Office.CommandBarButton)
    6.     Set oMnuSaveAs = btn
    7.     If btn Is Nothing Then
    8.         MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
    9.     End If
    10. End Sub
    11.  
    12. Private Sub Application_MAPILogonComplete()
    13.    
    14.     Dim oCBmnuTools As Office.CommandBarPopup
    15.     Dim oCBmnuSaveMe As Office.CommandBarButton
    16.    
    17.     '<ADD A MENU ITEM>
    18.     Set oCBmnuTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
    19.     Set oCBmnuSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
    20.     If TypeName(oCBmnuSaveMe) = "Nothing" Then
    21.         Set oCBmnuSaveMe = oCBmnuTools.Controls.Add(msoControlButton, 1, "888", , True)
    22.     End If
    23.     With oCBmnuSaveMe
    24.         .BeginGroup = True
    25.         .Caption = "Save Email As..."
    26.         .Enabled = True
    27.         .Style = msoControlCustom
    28.         .Tag = "888"
    29.         .Visible = True
    30.     End With
    31.     Call SyncMnuSaveAsButton(oCBmnuSaveMe)
    32.     '</ADD A MENU ITEM>
    33. End Sub
    34.  
    35. Private Sub oMnuSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    36.     MsgBox "Save Email As..."
    37. End Sub
    Last edited by RobDog888; Sep 2nd, 2006 at 09:18 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I add a custom menu item or toolbar button?

    Visio 2003 Toolbar Example...



    Visio 2003 VBA Code Example:

    VB Code:
    1. Option Explicit
    2.  
    3. Public WithEvents oCBBCustom As Office.CommandBarButton
    4.  
    5. Private Sub oCBBCustom_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    6.     MsgBox "Meow!"
    7. End Sub
    8.  
    9. Private Sub Document_Open()
    10.     Dim oCB As Office.CommandBar
    11.     Dim oCBBTools As Office.CommandBarPopup
    12.     Set oCB = Application.CommandBars("Standard")
    13.     Set oCBBCustom = oCB.Controls.Add(msoControlButton, 1, , 1, True)   'Place our custom one at the right end
    14.     With oCBBCustom                                                     'position from the left (1 based)
    15.         .BeginGroup = True
    16.         .Enabled = True
    17.         .TooltipText = "VB/Office Guru™"
    18.         .Style = msoButtonIcon
    19.         .Picture = LoadPicture("C:\Cat.bmp")
    20.         .Mask = LoadPicture("C:\CatMask.bmp")
    21.         .Visible = True
    22.     End With
    23.     'oCB.Reset 'To reset the menu.
    24. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width