2011-06-22 61 views
1

我有一个启动时加载的VSTO Outlook 2007插件。当它加载它的follwing:为什么我的VSTO Outlook加载项启动两次?

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 

当发送电子邮件的应用程序工作正常。但是,当我点击按钮时,添加火焰发生两次,当我使用上下文菜单时,它也会发射两次。

任何想法,为什么这可能是?

回答

1

我不完全确定这一点,但它看起来像你沉没的ContextMenuDisplay事件和CommandBarDisplay事件,然后创建一个按钮,并沉没它的单击事件每次的ContextMenuDisplay事件或CommandBarDisplay事件触发,这意味着您可能会多次钩住按钮单击事件,这会导致事件句柄不止一次被点击。我不相信每次事件触发时,Contextmenu或命令栏都会被销毁和重建。

我想你会想要创建按钮,并只沉没它的事件一次,测试按钮是否已经存在,如果它确实,什么也不做。

但它已经有一段时间,因为我已经挖成前景事件处理的vagueries ...

0

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

希望这会有所帮助

相关问题