StartDownloadsServiceSamplesWorkshopsContact DeutschEnglish
Awarded by
Microsoft since 2005:
mvp logo
VBOffice Info
Privacy Policy
Commandbar: Create a new bar with one button
Author: Michael BauerHomepage
Date: 20.01.2006Accessed: 36409

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.

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

ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the ... [more]


Access the master category list in the blink of an eye, share your categories in a network, get a reminder service, and ... [more]


SAM automatically sets the sender, signature, and folder for sent items, for instance based on the recipient ... [more]


OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or ... [more]