Outlook 2007 brought with it the all new ‘Ribbon Bar’ which has caused headaches to programmers the world over.
Now, using this code you can create a menu in Outlook. The menu, which contains one item, appears at the top of the application. When you click the menu item, the code displays a message that shows the menu item caption.
Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles Me.Startup
RemoveMenubar()
AddMenuBar()
End Sub
Private Sub AddMenuBar()
Try
menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
newMenuBar = menuBar.Controls.Add( _
Office.MsoControlType.msoControlPopup, _
Temporary:=False)
If newMenuBar IsNot Nothing Then
newMenuBar.Caption = “New Menu”
newMenuBar.Tag = menuTag
buttonOne = newMenuBar.Controls.Add( _
Office.MsoControlType.msoControlButton, _
Before:=1, Temporary:=True)
With buttonOne
.Style = Office.MsoButtonStyle.msoButtonIconAndCaption
.Caption = “Button One”
.FaceId = 65
.Tag = “c123”
End With
AddHandler buttonOne.Click, AddressOf ButtonOne_Click
newMenuBar.Visible = True
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
Public Sub ButtonOne_Click(ByVal buttonControl As Office. _
CommandBarButton, ByRef Cancel As Boolean)
MessageBox.Show(“You clicked: ” & buttonControl.Caption, _
“Custom Menu”, MessageBoxButtons.OK)
End Sub
Private Sub RemoveMenubar()
Try
‘ If the menu already exists, remove it.
Dim foundMenu As Office.CommandBarPopup = _
Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar. _
FindControl(Office.MsoControlType.msoControlPopup, _
System.Type.Missing, menuTag, True, True)
If foundMenu IsNot Nothing Then
foundMenu.Delete(True)
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub