自从几天以来,我一直在寻找解决方案,但我总是能得到解决问题的代码。我想通过VisualStudio在Outlook中创建几个按钮。这些按钮应该执行相同的子。但是当我用显示的代码创建按钮时,只有最后一个创建的按钮处理单击事件。
我使用的是VisualStudio (15.0)和Outlook (16.0,32位)
非常感谢你的帮助
霍格
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发布于 2020-05-05 19:53:40
如果希望为所有按钮处理事件,则必须保持对象引用处于活动状态。因此,基本上,您需要在全局范围内定义一个列表或按钮数组。
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的更多信息:
https://stackoverflow.com/questions/61621254
复制相似问题