PDA

Click to See Complete Forum and Search --> : [FAQ's: OD] How do I add a custom menu item or toolbar button?


RobDog888
Nov 7th, 2005, 12:00 AM
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...

http://vbforums.com/attachment.php?attachmentid=48591

Word 2003 VBA Code Example:
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

RobDog888
Aug 23rd, 2006, 05:10 PM
Here is another example using different logic.

Outlook 2003 Menu Item Example...

http://vbforums.com/attachment.php?attachmentid=48205

Outlook 2003 VBA Code Example:
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

RobDog888
Aug 23rd, 2006, 05:11 PM
Visio 2003 Toolbar Example...

http://vbforums.com/attachment.php?attachmentid=48593&stc=1

Visio 2003 VBA Code Example:
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