Compartir a través de


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

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

  • Anonymous
    January 01, 2003
    How to add a custom toolbar button and give it a tooltip .
  • Anonymous
    July 01, 2004
    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
  • Anonymous
    July 01, 2004
    The comment has been removed
  • Anonymous
    July 02, 2004
    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.
  • Anonymous
    August 05, 2004
    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.