由于几天来,我正在寻找一个解决方案,但我总是得到几乎我的问题的代码。我想通过VisualStudio在Outlook中创建几个按钮。这些按钮应该执行相同的子.但当我创建按钮与显示的代码,只有最后一个创建的按钮处理点击事件.我使用VisualStudio(15.0)和Outlook(16.0)。
我使用VisualStudio(15.0)和Outlook(16.0, 32bit)。
谢谢你的帮助
Holger
Public Class ThisAddIn
Dim ButtonControl As Office.CommandBarButton
Dim menuBar As Office.CommandBar
Dim newMenuBar As Office.CommandBarPopup
Private Sub ThisAddIn_Startup() Handles Me.Startup
Dim i As Integer
menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
newMenuBar = menuBar.Controls.Add(Office.MsoControlType.msoControlPopup, Temporary:=True)
If newMenuBar IsNot Nothing Then
newMenuBar.Caption = "Mailverschiebung"
For i = 0 To 3
ButtonControl = newMenuBar.Controls.Add
ButtonControl.Caption = "zeichen" & i
ButtonControl.Tag = "zeichen" & i
AddHandler ButtonControl.Click, AddressOf ButtonControl_Click
Next
End If
End Sub
Sub ButtonControl_Click()
MsgBox("Läuft")
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
End Class
如果你想让所有按钮的事件得到处理,你必须保持对象引用的活力。所以,基本上,你需要在全局范围内定义一个按钮的列表或数组。
Public Class ThisAddIn
Dim ButtonControl As Office.CommandBarButton
Dim ButtonControls As List(Of Office.CommandBarButton) = new List(Of Office.CommandBarButton)()
Dim menuBar As Office.CommandBar
Dim newMenuBar As Office.CommandBarPopup
Private Sub ThisAddIn_Startup() Handles Me.Startup
Dim i As Integer
menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
newMenuBar = menuBar.Controls.Add(Office.MsoControlType.msoControlPopup, Temporary:=True)
If newMenuBar IsNot Nothing Then
newMenuBar.Caption = "Mailverschiebung"
For i = 0 To 3
ButtonControl = newMenuBar.Controls.Add
ButtonControl.Caption = "zeichen" & i
ButtonControl.Tag = "zeichen" & i
AddHandler ButtonControl.Click, AddressOf ButtonControl_Click
ButtonControls.Add(ButtonControl)
Next
End If
End Sub
Sub ButtonControl_Click()
MsgBox("Läuft")
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
End Class
请注意,命令栏已被废弃,不应该用于自定义Outlook用户界面。相反,你应该使用Fluent UI(又称Ribbon UI)。在下面的系列文章中阅读更多关于新用户界面的信息。