Question : Outlook 2007 Add-in  menu button click event... Strange behavior.

simple add-in for outlook 2007...

2 events (one for menu button .click another for .newMailEx). Strange behavior, newButton.click event only fires once.

What am I doing wrong/missing?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
Public Class ThisAddIn
    Dim _Explorers As Outlook.Explorers
    Dim _Inspectors As Outlook.Inspectors
    Dim _NameSpace As Outlook.NameSpace
    Dim settingsFrm As Form = New frmSettings
 
    Public Sub InitializeMenu()
        Dim _menuBar As Office.CommandBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar
        Dim _helpMenuIndex As Object
        Try
            _helpMenuIndex = _menuBar.Controls("Help").Index
        Catch ex As ArgumentException
            _helpMenuIndex = _menuBar.Controls.Count
        End Try
        
        Dim _topMenu As Office.CommandBarPopup = _menuBar.Controls.Add(Office.MsoControlType.msoControlPopup, Type.Missing, Type.Missing, _helpMenuIndex, True)
        If _topMenu IsNot Nothing Then
            _topMenu.Caption = "menu1"
            Try
                Dim newButton As Microsoft.Office.Core.CommandBarButton
                newButton = CType(_topMenu.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton), Microsoft.Office.Core.CommandBarButton)
                newButton.Caption = "button1"
                newButton.FaceId = 1044
                AddHandler newButton.Click, AddressOf buttonOne_Click
            Catch ex As System.Exception
                Debug.Print("Could Not Create Button ""button1""...")
            End Try
            _topMenu.Visible = True
        End If
    End Sub
    Public Sub buttonOne_Click(ByVal barButton As Microsoft.Office.Core.CommandBarButton, ByRef someBool As Boolean)
        If settingsFrm.Visible = False Then
            settingsFrm.Show()
        End If
    End Sub
    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        Me.InitializeMenu()
        _NameSpace = Me.Application.GetNamespace("MAPI")
        _Explorers = Me.Application.Explorers
        _Inspectors = Me.Application.Inspectors
        AddHandler _Explorers.Application.NewMailEx, AddressOf Application_NewMail
    End Sub
    Sub Application_NewMail(ByVal EntryID As String)
        Dim newMail As Outlook.MailItem = _Explorers.Application.Session.GetItemFromID(EntryID, System.Reflection.Missing.Value)
 
        Dim mSubject As String = newMail.Subject
        Dim mBodyType As Outlook.OlBodyFormat = newMail.BodyFormat
        Dim mBody As String = newMail.Body
        Dim mHTMLBody As String = newMail.HTMLBody
        Dim mAttachements As Outlook.Attachments = newMail.Attachments
        Dim mSenderAddressType As String = newMail.SenderEmailType
        Dim mSenderAddress As String = newMail.SenderEmailAddress
        Dim mSenderName As String = newMail.SenderName
    End Sub
 
    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
 
    End Sub
 
End Class

Answer : Outlook 2007 Add-in  menu button click event... Strange behavior.

Just make
_menuBar As Office.CommandBar
a field rather than local variable of InitializeMenu.
//code snipet  to change line 1 to 8
Public Class ThisAddIn
    Dim _Explorers As Outlook.Explorers
    Dim _Inspectors As Outlook.Inspectors
    Dim _NameSpace As Outlook.NameSpace
    Dim settingsFrm As Form = New frmSettings
    Dim _menuBar As Office.CommandBar
 
    Public Sub InitializeMenu()
        _menuBar = Me.Application.ActiveExplorer().CommandBars.ActiveMenuBar

You need an active instance of the menu bar for your add-in to work OK.
Random Solutions  
 
programming4us programming4us