Create Outlook toolbar buttons using VBA

November 23, 2009JPNo CommentsRate This ArticlenewLinks to this article


under construction

    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).

Function AddToolbarButton(Caption As String, _
                       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:

Sub maketoolbarbutton()

  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:

Function AddToolbarButton(Caption As String, _
                       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:

Sub MakeToolbarButton()

  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.

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

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].




Site last updated August 24, 2010 @ 5:56 pm