微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么我的VSTO Outlook加载项触发两次?

| 我有一个在启动时加载的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,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,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
发送电子邮件后,该应用程序可以正常运行。但是,当我单击“按钮”时,则add in触发2倍,而当我使用上下文菜单时,其也会触发2倍。 知道为什么会这样吗?     

解决方法

        我对此并不完全确定,但是看起来就像您正在沉浸ContextMenuDisplay事件和CommandBarDisplay事件,然后创建一个按钮并沉浸其click事件的事件每次ContextMenuDisplay事件或CommandBarDisplay事件触发时,这意味着您可能多次钩住按钮单击事件,这将导致多次单击事件调用事件句柄。我不认为每次事件触发时Contextmenu或命令栏都会被破坏并重建。 我认为您只想创建按钮并将其事件下沉一次,测试按钮是否已经存在,是否存在,则什么也不做。 但是自从我深入研究Outlook事件处理的问题以来已经有一段时间了...     ,        我的C#Outlook插件遇到了类似的问题。我相信当我编译代码并对其进行调试时,该插件会从我的dev目录中向Outlook注册。然后,当我运行安装程序时,它将再次注册,因此当我打开Outlook动作时,它会启动2倍。我不得不手动去从我的dev目录中删除加载的插件。 希望这可以帮助     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。