为什么我的VSTO Outlook加载项会被触发两次?

时间:2011-06-22 00:06:13

标签: vb.net vsto outlook-addin

我有一个在启动时加载的VSTO Outlook 2007插件。当它加载时,它会执行以下操作:

    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

然后AddHandlers执行以下操作:

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

发送电子邮件时,应用程序正常运行。但是当我点击按钮时,添加内容会激活2次,当我使用上下文菜单时,它会激发2次。

知道为什么会这样吗?

3 个答案:

答案 0 :(得分:1)

我对此并不完全确定,但它看起来就像你正在沉没ContextMenuDisplay事件和CommandBarDisplay事件,然后创建一个按钮,并在每次触发ContextMenuDisplay事件或CommandBarDisplay事件时触发它的click事件,这意味着您可能会多次挂钩按钮单击事件,这会导致多次单击调用事件句柄。我不相信每次事件触发时都会破坏并重建Contextmenu或命令栏。

我认为你想创建按钮并只将它的事件吸收一次,测试按钮是否已经存在,如果已经存在,那就什么都不做。

但是我已经有一段时间了,因为我已经挖掘了前景事件处理的迷惑......

答案 1 :(得分:1)

我在使用C#的outlook插件时遇到过类似的问题。我相信当我编译代码并对其进行调试时,插件会从我的dev目录中注册到Outlook。然后,当我运行设置时,它会再次注册,所以当我打开Outlook时,操作会被激活2倍。我不得不手动从我的开发目录中删除加载的插件。

希望这有帮助

答案 2 :(得分:0)

我注意到我的加载项同时触发了 ThisAddIn_Startup 和 ThisAddIn_Shutdown 2 次。这个周末我花了大约 10 个小时试图解决这个问题。我什至添加了一个带有消息框的增量计数器:消息框弹出 2 次,但计数器没有增加。

作为开发人员,您的机器可能有多个测试项目,可能还有一些废弃的旧项目。我有一个相同清单文件名的旧插件,但位于不同的存储库位置。经过几次注册表搜索

计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visio\Addins\OLD_ADDIN

*删除旧的注册表项并快速处理,事件仅触发 1 次

相关问题