VisualStudio 的 Outlook 插件 - 按钮事件处理程序

Outlook Addin per VisualStudio - Button Eventhandler

几天以来,我一直在寻找解决方案,但我几乎总能找到解决问题的代码。 我想通过 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

如果要处理所有按钮的事件,则必须保持对象引用处于活动状态。因此,基本上,您需要在全局范围内定义一个按钮列表或按钮数组。

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 的更多信息: