因为几天来我一直在寻找解决方案,但我总能找到解决问题的代码。我想通过VisualStudio在Outlook中创建几个按钮。这些按钮应该执行相同的sub。但是,当我使用显示的代码创建按钮时,只有最后创建的按钮处理单击事件。
我使用的是VisualStudio(15.0(和Outlook(16.0,32位(
非常感谢您的帮助
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 UI。相反,您应该使用Fluent UI(也称为Ribbon UI(。在以下系列文章中阅读更多关于新UI的信息:
- 为开发人员自定义2007 Office Fluent功能区(第1部分,共3部分(
- 为开发人员自定义2007 Office Fluent功能区(第2部分,共3部分(
- 为开发人员自定义2007 Office Fluent功能区(第3部分,共3部分(