Create a New Commandbar with one Button

A sample for how to create a your own toolbar in Outlook 2007 (or earlier) with command buttons.

Last modified: 2006/01/20 | Accessed: 43.639  | #16
◀ Previous sample Next sample ▶
Determine the "identity" of your emails. Set with SAM the sender, signature and folder for sent items with the help of rules.

The CommandBars of the Office library allow you to create your own toolbars and menubars with buttons. The sample also shows how to create a variable so that you can receive an event of that object, in this case a click event of a button.

It's recommended to always create the objects temporarily, so that they are being removed automatically as soon as you close the document or application.

tip  How to add macros to Outlook
Private WithEvents Button As Office.CommandBarButton

Private Sub Application_Startup()
  Dim oExplorer As Outlook.Explorer

  Set oExplorer = Application.ActiveExplorer
  Set Button = CreateCommandBarButton(oExplorer.CommandBars)
End Sub

Private Sub Button_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
  MsgBox "Click: " & Ctrl.Caption
End Sub

Private Function CreateCommandBarButton(oBars As Office.CommandBars) As Office.CommandBarButton
  On Error Resume Next
  Dim oMenu As Office.CommandBar
  Dim oBtn As Office.CommandBarButton
  Const BAR_NAME As String = "YourCommandBarName"
  Const CMD_NAME As String = "YourButtonName"

  Set oMenu = oBars(BAR_NAME)
  If oMenu Is Nothing Then
    Set oMenu = oBars.Add(BAR_NAME, msoBarTop, , True)
    Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, , True)
    oBtn.Caption = CMD_NAME
    oBtn.Tag = CMD_NAME

    Set oBtn = oMenu.FindControl(, , CMD_NAME)
    If oBtn Is Nothing Then
      Set oBtn = oMenu.Controls.Add(msoControlButton, , CMD_NAME, , True)
    End If
  End If

  oMenu.Visible = True
  Set CreateCommandBarButton = oBtn
End Function
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.
email  Send a message