【问题标题】:Why is my VSTO Outlook add-in firing twice?为什么我的 VSTO Outlook 加载项会触发两次?
【发布时间】:2011-09-19 23:28:00
【问题描述】:

我有一个在启动时加载的 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 次。

知道为什么会这样吗?

【问题讨论】:

    标签: vb.net vsto outlook-addin


    【解决方案1】:

    我对此并不完全确定,但看起来您正在接收 ContextMenuDisplay 事件和 CommandBarDisplay 事件,然后创建一个按钮并将其单击事件每次 ContextMenuDisplay 事件或 CommandBarDisplay 事件触发,这意味着您可能会多次钩住按钮单击事件,这将导致在单击时多次调用事件句柄。我不相信每次触发事件时上下文菜单或命令栏都会被破坏和重建。

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

    但我已经有一段时间没有深入了解 Outlook 事件处理的模糊性了......

    【讨论】:

      【解决方案2】:

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

      希望对你有帮助

      【讨论】:

        【解决方案3】:

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

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

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

        *删除了旧的注册表项,并且事件只触发了 1 次

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-08
          • 1970-01-01
          • 1970-01-01
          • 2022-01-24
          • 2017-11-04
          相关资源
          最近更新 更多