Create Outlook toolbar buttons using VBA
November 23, 2009 • JP • No Comments • Rate This Article
• Links to this article

Warning: Toolbars may be under construction. Just kidding.
You can create toolbar buttons using a bit of VBA programming in Outlook's VB IDE. I've adapted some code found at Ken Slovak's site so you can programmatically create buttons on the built-in toolbars, or a custom toolbar you've created.
You can set the button's Caption (the actual text appearing on the button), the Tool Tip (the text that appears when you hover over the button), the name of the macro that runs when the button is clicked, the name of the toolbar where the button should appear (defaults to Standard), and the Face ID of the button.
I defaulted to Face ID 325, which is a small envelope (it is Outlook, after all), but if you want something different, visit the Face ID Toolbar Generator page or download JMT Utilities (it has a Face ID browser). Both are for Excel (although you can adapt my Face ID code for Outlook as well).
toolTip As String, macroName As String, _
Optional toolbarName As String = "Standard", _
Optional FaceID As Long = 325)
Dim objBar As Office.CommandBar
Dim objButton As Office.CommandBarButton
Set objBar = ActiveExplorer.CommandBars(toolbarName)
Set objButton = objBar.Controls.Add(msoControlButton)
With objButton
.Caption = Caption
.OnAction = macroName
.TooltipText = toolTip
.FaceID = FaceID
.Style = msoButtonIconAndCaption
.BeginGroup = True
End With
End Function
Sample usage:
Call AddToolbarButton("My button", "Click here", "Macro Name")
End Sub
Note that I didn't abstract out the Style property; I assumed you want to see the icon and the caption at the same time. You can always change this as follows:
toolTip As String, macroName As String, _
Optional toolbarName As String = "Standard", _
Optional FaceID As Long = 325, _
Optional buttonStyle As MsoButtonStyle = msoButtonIconAndCaption)
Dim objBar As Office.CommandBar
Dim objButton As Office.CommandBarButton
Set objBar = ActiveExplorer.CommandBars(toolbarName)
Set objButton = objBar.Controls.Add(msoControlButton)
With objButton
.Caption = Caption
.OnAction = macroName
.TooltipText = toolTip
.FaceID = FaceID
.Style = buttonStyle
.BeginGroup = True
End With
End Function
Then call the function like this:
Call AddToolbarButton("My button", "Click here", "Macro Name", , , msoButtonIconAndCaption)
End Sub
Now you can go button-crazy, but it's addictive so don't say I didn't warn you.
In an upcoming post we'll review a real application of this function, and after that we'll go over how you can call a macro that takes parameters from a toolbar button in Outlook.
↑ Scroll to topPrevious Post: Shorten urls with bit.ly web API and VBA
Next Post: Meeting Replies in Outlook 2003




Speak Your Mind
Tell us what you're thinking...Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].