Search code examples
visual-studiooutlook-addinaddhandler

Outlook Addin per VisualStudio - Button Eventhandler


since several days I am searching for a solution, but I always get nearly the code for my problem. I want to create several buttons within Outlook via VisualStudio. These buttons should execute the same sub. But when I create the buttons with the shown code, only the last created button handles the click-event.

I'm using VisualStudio (15.0) and Outlook (16.0, 32bit)

Many thanks for your help

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

Solution

  • You must keep the object reference alive if you want to get events handled for all buttons. So, basically, you need to define a list or array of buttons at the global scope.

    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
    

    Be aware, command bars were deprecated and shouldn't be used for customnizing the Outlook UI. Instead, you should use the Fluent UI (aka Ribbon UI). Read more about the new UI in the following series of articles: