Tuesday, October 14, 2008

Adding buttons to outlook

From
http://msdn.microsoft.com/en-us/library/ms268864(VS.80).aspx


Dim newToolBar As Office.CommandBar
Dim firstButton As Office.CommandBarButton
Dim secondButton As Office.CommandBarButton
Dim selectExplorers As Outlook.Explorers

Private Sub ThisApplication_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
selectExplorers = Me.Explorers()
AddHandler selectExplorers.NewExplorer, AddressOf _
Me.NewExplorer_Event
AddToolbar()
End Sub

Private Sub NewExplorer_Event(ByVal new_Explorer _
As Outlook.Explorer)
new_Explorer.Activate()
newToolBar = Nothing
Call Me.AddToolbar()
End Sub

Private Sub AddToolbar()

Dim button_1 As Office.CommandBarButton
Dim button_2 As Office.CommandBarButton
If newToolBar Is Nothing Then
Dim cmdBars As Office.CommandBars = _
Me.ActiveExplorer().CommandBars
newToolBar = cmdBars.Add("NewToolBar", _
Office.MsoBarPosition.msoBarTop, False, True)
End If
Try
button_1 = CType(newToolBar.Controls.Add(1), _
Office.CommandBarButton)
With button_1
.Style = Office.MsoButtonStyle.msoButtonCaption
.Caption = "Button 1"
.Tag = "Button1"
End With
If Me.firstButton Is Nothing Then
Me.firstButton = button_1
AddHandler firstButton.Click, AddressOf ButtonClick
End If
button_2 = CType(newToolBar.Controls.Add(1), _
Office.CommandBarButton)
With button_2
.Style = Office.MsoButtonStyle.msoButtonCaption
.Caption = "Button 2"
.Tag = "Button2"
End With
If Me.secondButton Is Nothing Then
Me.secondButton = button_2
AddHandler secondButton.Click, AddressOf ButtonClick
End If
newToolBar.Visible = True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub ButtonClick(ByVal ctrl As Office.CommandBarButton, _
ByRef Cancel As Boolean)
MessageBox.Show("You clicked: " + ctrl.Caption)
End Sub

No comments: