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

Vb.net 为什么我的VSTO Outlook加载项会触发两次?,vb.net,vsto,outlook-addin,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

我有一个在启动时加载的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
然后,AddHandler执行以下操作:

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次


知道为什么会这样吗?

我不完全确定,但看起来您正在取消ContextMenuDisplay事件和CommandBarDisplay事件,然后创建一个按钮,并在每次触发ContextMenuDisplay事件或CommandBarDisplay事件时取消其click事件,这意味着您可能会多次钩住按钮单击事件,这将导致在单击时多次调用事件句柄。我不相信Contextmenu或commandbar在每次事件触发时都会被销毁和重建

我认为您应该只创建一次按钮并接收它的事件,测试按钮是否已经存在,如果已经存在,则什么也不做


但是我已经有一段时间没有深入研究outlook事件处理的模糊性了。

我在使用C#的outlook插件时遇到了类似的问题。我相信当我编译并调试代码时,插件会从我的dev目录注册到Outlook。然后,当我运行安装程序时,它会再次注册,因此当我打开Outlook时,操作会被激发2倍。我不得不手动从我的dev目录中删除加载的插件


希望这有帮助

我注意到我的加载项同时启动和关闭了两次此加载项。这个周末我花了大约10个小时试图解决这个问题。我甚至用messagebox添加了一个递增计数器:messagebox弹出了2次,计数器没有递增

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

计算机\HKEY\U本地\U计算机\SOFTWARE\Microsoft\Visio\Addins\OLD\u ADDIN

*已删除旧注册表项和presto,事件仅激发1次