|
-
Nov 7th, 2005, 12:00 AM
#1
[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:
Option Explicit
'<CREATE THE EVENT HANDLER>
Public WithEvents oCBBCustom As Office.CommandBarButton
'<EVENT PROCEDURE>
Private Sub oCBBCustom_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox "Meow!"
End Sub
Private Sub Document_Open()
Dim oCB As Office.CommandBar
Dim oCBBTools As Office.CommandBarPopup
'<DRILL DOWN THE COMMANDBAR OBJECT HEIRARCHY>
Set oCB = Application.CommandBars("Menu Bar")
Set oCBBTools = oCB.Controls("&Tools")
'<ADD A NEW BUTTON>
Set oCBBCustom = oCBBTools.Controls.Add(msoControlButton, 1, , , True) 'Place our custom one at the bottom
With oCBBCustom 'position by leaving it empty or
.Caption = "VB/Office Guru SpellChecker™" 'specify a position. (0 based)
.BeginGroup = True
.Enabled = True
.Visible = True
End With
'<USE THE .RESET METHOD TO RESET THE COMMAND BAR TO ORIGINAL SETTINGS>
'oCB.Reset 'To reset the menu.
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|