1
votes

I have a VSTO Outlook 2007 addin that loads on startup. When it loads it does the follwing:

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        explorer = Me.Application.ActiveExplorer()
        AddHandler Application.ItemContextMenuDisplay, AddressOf Application_ItemContextMenuDisplay
        AddHandler Application.Startup, AddressOf Application_CommandBarMenuDisplay
    End Sub

Then after this the AddHandlers does the following:

Sub Application_CommandBarMenuDisplay()

            Dim cBar As Office.CommandBar = explorer.CommandBars("Standard")
            btnCommandBarMenu = CType(cBar.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, True), Office.CommandBarButton)

            With btnCommandBarMenu
                .BeginGroup = True
                .Style = MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "File TNRP Email"
                .Tag = "File TNRP Email"
                .Picture = IPictureDisp.FromImage(My.Resources.label16)
                .Mask = IPictureDisp.MaskFromImage(My.Resources.label16)
            End With

            AddHandler btnCommandBarMenu.Click, AddressOf btn_CommandBarMenuClick

    End Sub

Sub Application_ItemContextMenuDisplay(ByVal CommandBar As Microsoft.Office.Core.CommandBar, ByVal Selection As Microsoft.Office.Interop.Outlook.Selection)

            btnContextMenu = CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, True)

            With btnContextMenu
                .BeginGroup = True
                .Visible = True
                .Style = MsoButtonStyle.msoButtonIconAndCaption
                .Caption = "File TNRP Email"
                .Tag = "File TNRP Email"
                .Picture = IPictureDisp.FromImage(My.Resources.label16)
                .Mask = IPictureDisp.MaskFromImage(My.Resources.label16)
            End With

            AddHandler btnContextMenu.Click, AddressOf btn_ContextMenuClick

End Sub

When the email is sent the app works fine. But when I click on the Button, then the add in fires 2x times and when I use the context menu it aslo fires 2x times.

Any idea why this might be?

3

3 Answers

1
votes

I'm not completely sure about this, but it LOOKS like you're sinking the ContextMenuDisplay event and the CommandBarDisplay event, and then creating a button and sinking it's click event EACH TIME the ContextMenuDisplay event or CommandBarDisplay event fires, which means you might hook the button click event any number of times, which would then cause the event handle to be called on a click more than once. I don't believe the Contextmenu or commandbar is destroyed and rebuilt each time that event fires.

I'd think you'd want to create the button and sink it's events only once, testing if the button already existed and if it does, just do nothing.

But it's been a while since I've dug into the vagueries of outlook event handling...

1
votes

I've encountered similar problem with my outlook plugin with C#. I believe when I compile the code and debug it, the plugin gets registered with Outlook from my dev directory. And then when I run through the setup it gets registered again, so when I open up Outlook actions get fired up 2X. I had to manually go and remove the loaded plugin from my dev directory.

Hope this helps

0
votes

I noticed my Add-In was firing both ThisAddIn_Startup and ThisAddIn_Shutdown 2 times. I spent about 10 hours this weekend trying to solve this issue. I even added an increment counter with a messagebox : the messagebox popped up 2 times and the counter did NOT increment.

As a developer your machine might have more than a couple of test projects and maybe some old abandoned ones. I had an older Add-in of the same manifest file name but in a different repository location. After a couple of searches through the registry

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visio\Addins\OLD_ADDIN

*deleted the old registry entry and presto, event only fires 1 time