Create Outlook toolbar buttons using VBA

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 to learn more about VBA. Keep Reading »



Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 5 Comment(s) on Create Outlook toolbar buttons using VBA:

  1. Hi,

    I had a small requirement similar kind of above mentioned "Create Outlook toolbar buttons using VBA".

    1.But my requirement is when a end user clicks the button a mail should go to our "IT helpdesk" with blank subject line as user writes the problem and also with one more blank where user write the phone number.

    2.The mail should contain the a row with computer's host name either should capture from Registry or C:/Program files

    3.The mail should even contain the Ip address (Dhcp/Static) captures from Registry.

    4.Where to deploy the same either in exchange or we can push the application in a C:/Prog files thru Microsoft Systems Management Server.

    Thanks

  2. This is great. I really got to know many things from here but how do i delete this button again using vba?

    • Set a reference to the commandbar object that you set in the original code.

      Then you can either loop through all the controls and look for the one with the same caption, or you might be able to set a reference to the button directly using its Caption Property. After that, I'm sure there's a delete method you can call to remove the button.

  3. This is exactly what I was looking for. My only problem is that I need to have the .onaction command reference a macro (i.e. sub) but I somehow need to pass the .captiont to it. I have not been able to do this successfully since you need to put the macro name in " " (i.e. .onaction="mysub")

    Can you help?

    Moiz

This article is closed to any future comments.
Random Data Generator