|
-
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 
-
Aug 23rd, 2006, 04:10 PM
#2
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:
Option Explicit
'Behind ThisOutlookSession
Public WithEvents oMnuSaveAs As Office.CommandBarButton
Private Sub SyncMnuSaveAsButton(btn As Office.CommandBarButton)
Set oMnuSaveAs = btn
If btn Is Nothing Then
MsgBox "Sync. of '" & btn.Caption & "' button event failed!", vbCritical + vbOKOnly
End If
End Sub
Private Sub Application_MAPILogonComplete()
Dim oCBmnuTools As Office.CommandBarPopup
Dim oCBmnuSaveMe As Office.CommandBarButton
'<ADD A MENU ITEM>
Set oCBmnuTools = Application.ActiveExplorer.CommandBars("Menu Bar").Controls("&Tools")
Set oCBmnuSaveMe = Application.ActiveExplorer.CommandBars("Menu Bar").FindControl(msoControlButton, 1, "888", True, True)
If TypeName(oCBmnuSaveMe) = "Nothing" Then
Set oCBmnuSaveMe = oCBmnuTools.Controls.Add(msoControlButton, 1, "888", , True)
End If
With oCBmnuSaveMe
.BeginGroup = True
.Caption = "Save Email As..."
.Enabled = True
.Style = msoControlCustom
.Tag = "888"
.Visible = True
End With
Call SyncMnuSaveAsButton(oCBmnuSaveMe)
'</ADD A MENU ITEM>
End Sub
Private Sub oMnuSaveAs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox "Save Email As..."
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 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 
-
Aug 23rd, 2006, 04:11 PM
#3
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:
Option Explicit
Public WithEvents oCBBCustom As Office.CommandBarButton
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
Set oCB = Application.CommandBars("Standard")
Set oCBBCustom = oCB.Controls.Add(msoControlButton, 1, , 1, True) 'Place our custom one at the right end
With oCBBCustom 'position from the left (1 based)
.BeginGroup = True
.Enabled = True
.TooltipText = "VB/Office Guru™"
.Style = msoButtonIcon
.Picture = LoadPicture("C:\Cat.bmp")
.Mask = LoadPicture("C:\CatMask.bmp")
.Visible = True
End With
'oCB.Reset 'To reset the menu.
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 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
|