KC Lemson

By KC Lemson [MS]

How to add a custom toolbar button and give it a tooltip

How to add a custom toolbar button and give it a tooltip

  • Comments 5
  • Likes

Bob asked in the comments on this article if it's possible to add a tooltip to a button. Here's one way to do it programmatically for your own buttons. The below example adds a custom button to the standard toolbar and assigns it a tooltip. Note that the button does not actually do anything in this sample - in order to make it do anything, you'd need to change “NameOfFunctionToCallOnClick“ in the first sub below to call your own macro.

First, follow the instructions in this article with this code:

Sub AddCustomButton()
    'Adds a custom button to the standard toolbar if it's not there already.
    'Clicking on this button calls a custom action that would have to be
    'defined in a separate sub, see the .OnAction line below.
    Dim cbb As CommandBarButton
    Dim cbStandard As CommandBar
    Dim cbExist As Boolean
   
    'The Standard toolbar is the one with "New", "Send/Receive", etc on it.
    Set cbStandard = ActiveExplorer.CommandBars("Standard")
    cbExist = IsMenuThere("Standard", "CustomButton")
   
    'If the option is not already on the menu, add it
    If cbExist = False Then
        Set cbb = cbStandard.Controls.Add(msoControlButton)
        cbb.Caption = "Custom&Button"
        cbb.OnAction = "NameOfFunctionToCallOnClick"
        cbb.TooltipText = "Tooltip for this button"
    End If
   
    Set cbb = Nothing
    Set cbEdit = Nothing
    Set cbStandard = Nothing

End Sub

Public Function IsMenuThere(sMenu As String, sName As String) As Boolean
    'Returns true if menu sName exists in sMenu
    Dim cbb As CommandBar
    Dim cbControl As CommandBarControl
   
    IsMenuThere = False
   
    Set cbb = ActiveExplorer.CommandBars(sMenu)
   
    'Cycles through the given commandbar, checking the captions to see
    'if they match the one we're looking for
    For Each cbControl In cbb.Controls
        If cbControl.Caption = sName Then
        IsMenuThere = True
        Exit Function
        End If
    Next
   
    Set cbb = Nothing
    Set cbControl = Nothing

End Function

Note: The IsMenuThere function comes from Outlook MVP Ken Slovak's Outlook 2000 programming book. I read it many years ago while teaching myself how to write code in Outlook, and I have used it countless times since then. Highly recommended book; very readable and easy to pick and choose parts out of it to learn what you want to learn (I picked it up with the goal of finding out how to add a toolbar button programmatically for example =).

Comments
  • Am attempting to use your code to auto-generate a custom toolbar button however am having problems passing the Outlook appicaton obect into your code.

    Does the Outlook app have to be open when you run your code or can I automate the opening of Outlook, then pass this into your code for creation of the custom toolbar button

    Thanks in advance for any information you may be able to provide

    regards

    Steve Beynon

  • Here is the revised code that I am using to open and create the custom toolbar button on the Standard toolbar

    Public Function StartOutlook(Optional ProfileName) As Outlook.Application

    Dim lobjOutlookApp As Outlook.Application
    Dim lobjSaveEmailToSPFButton As CommandBarButton
    Dim lobjStandard As CommandBar
    Dim cbExist As Boolean

    'obtain the os username and set as ProfileName
    ProfileName = Environ("USERNAME")

    On Error Resume Next

    Set lobjOutlookApp = GetObject(, "Outlook.Application")

    'On Error GoTo InstallToolbarOutlook_ERROR

    If lobjOutlookApp Is Nothing Then

    Set lobjOutlookApp = CreateObject("Outlook.Application")
    lobjOutlookApp.Session.Logon ProfileName, , False, True

    End If

    ' now setup the custom toolbar button on the standard toolbar

    'The Standard toolbar is the one with "New", "Send/Receive", etc on it.
    Set lobjStandard = lobjOutlookApp.ActiveExplorer.CommandBars("Standard")

    "NB: The line above sets lobjStandard as Nothing even though lobjOutlookApp is set to Outlook"

    cbExist = IsMenuThere("Standard", "CustomButton")

    'If the option is not already on the menu, add it
    If cbExist = False Then

    Set lobjSaveEmailToSPFButton = lobjStandard.Controls.Add(msoControlButton)

    With lobjSaveEmailToSPFButton
    .BeginGroup = True
    .Caption = "Custom&Button"
    .OnAction = "SaveEmailToSPF_Click"
    .ToolTipText = "Save Email To SPF"
    .Style = msoButtonIconAndCaption
    End With

    End If

    InstallToolbarOutlook_EXIT:
    'close all applications
    Set lobjOutlookApp = Nothing
    Unload frmInstallToolbar
    pobjOutlookApp.Quit
    Exit Function

    InstallToolbarOutlook_ERROR:
    InstallToolbarOutlook = False
    GoTo InstallToolbarOutlook_EXIT

    End Function

  • In my case, I was running this code from within the VBA editor in Outlook, so yes it was open. I'm not such an expert in this area that I can tell you exactly what to do here though, I recommend checking out www.outlookcode.com.

  • How to add a custom toolbar button and give it a tooltip .

  • Do you have any advice how to add transparent icon to Outlook2000 toolbar and menu?
    I have created COM Addin in VC#.NET that is supposed to work for Outlook2000, XP and 2003. Unfortunately - Outlook2000 handles icons differently then OutlookXP and Outlook2003.
    I have found an article on MSDN which describes a technique how to accomplish this using Win32 API, but I want to avoid using Win32.