Dim commandBar As Office.CommandBar
Dim WithEvents button As Office.CommandBarButton
Const TOOLBARNAME As String = "ExampleBar"
Const REGROOT As String = "Software\YourCompany\YourApp"
Dim WithEvents explorerEvents As Outlook.ExplorerEvents_Event
Dim explorer As Outlook.Explorer
Private Sub ThisAddIn_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
explorer = Me.Application.ActiveExplorer()
If (explorer IsNot Nothing) Then
commandBar = Me.Application.ActiveExplorer.CommandBars.Add( _
TOOLBARNAME, _
Office.MsoBarPosition.msoBarFloating, _
False, _
True)
button = TryCast(commandBar.Controls.Add( _
Office.MsoControlType.msoControlButton, _
System.Type.Missing, System.Type.Missing, _
1, True), Office.CommandBarButton)
button.Style = _
Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption
button.Caption = "Button 1"
button.Tag = "newButton"
LoadCommandBarSettings()
End If
End Sub
Private Sub SaveCommandBarSettings()
MySettings.Default("CommandBarTop") = commandBar.Top
MySettings.Default("CommandBarLeft") = commandBar.Left
MySettings.Default("CommandBarVisible") = commandBar.Visible
MySettings.Default("CommandBarPosition") = CInt(commandBar.Position)
MySettings.Default("CommandBarRowIndex") = commandBar.RowIndex
MySettings.Default.Save()
End Sub
Private Sub LoadCommandBarSettings()
Dim position As Microsoft.Office.Core.MsoBarPosition = _
CType(MySettings.Default("CommandBarPosition"), _
Microsoft.Office.Core.MsoBarPosition)
Dim rowIndex As Integer = _
CInt(MySettings.Default("CommandBarRowIndex"))
commandBar.RowIndex = rowIndex
Dim top As Integer = _
CInt(MySettings.Default("CommandBarTop"))
commandBar.Top = _
CInt(IIf(top <> 0, top, _
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2))
Dim left As Integer = _
CInt(MySettings.Default("CommandBarLeft"))
commandBar.Left = _
CInt(IIf(left <> 0, left, _
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2))
Dim visible As Boolean = _
CBool(MySettings.Default("CommandBarVisible"))
commandBar.Visible = visible
End Sub
Private Sub Click(ByVal ctrl As _
Microsoft.Office.Core.CommandBarButton, _
ByRef cancelDefault As Boolean) Handles Button.Click
System.Windows.Forms.MessageBox.Show("Hello World!")
End Sub
Sub ThisAddIn_Close() Handles explorerEvents.Close
SaveCommandBarSettings()
End Sub