Home > Mobile >  VSTO Outlook Add-in: Cannot use Explorer_Activate event due to ambiguity
VSTO Outlook Add-in: Cannot use Explorer_Activate event due to ambiguity

Time:02-26

I am writing an Outlook 365 32 bit VSTO add-in that can perform code when the to-do list explorer is activated. Ideally, I would simply do something like the following:

Private WithEvents OlExplr As Outlook.Explorer

Private Sub ThisAddIn_Startup() Handles Me.Startup
     OlExplr = Application.ActiveExplorer
End Sub

Private Sub OlExplr_Activate() Handles OlExplr.Activate
'''Do some stuff here
End Sub

Unfortunately, Activate is both a method and event for the Explorer class, and as such, compile errors will occur if I attempt to implement as above. I have seen several examples (below) of how to handle this ambiguity in C#, but I cannot translate to vb.net effectively, nor do I really understand what they mean by "cast OutlookExplorer variable above to ExplorerEvents interface". I know I will need to use ExplorerEvents_10_ActivateEventHandler or possibly ExplorerEvents_10_Event, but the actual implementation is beyond my current skill level.

VSTO Outlook AddIn: Cannot use Explorer Close event

What is the difference between _Application and Application

Can someone please explain what casting would mean in this context, and how to circumvent the ambiguity issue?

Edit:

Following Dmitry's answer, I got the following, though it is not triggering the event still (no errors are being flagged at least...)

Public Class ThisAddIn
Private WithEvents OlExplr As Microsoft.Office.Interop.Outlook.Explorer
Public Delegate Sub ExplorerEvents_10_ActivateEventHandler(ByRef sender As Object)
Public Event OlExplr_Activate As ExplorerEvents_10_ActivateEventHandler

Private Sub ThisAddIn_Startup() Handles Me.Startup
    OlExplr = Application.ActiveExplorer
    AddHandler OlExplr_Activate, New ExplorerEvents_10_ActivateEventHandler(AddressOf OlExplr.Activate)
End Sub

Private Sub ThisAddIn_OlExplr_Activate(ByRef sender As Object) Handles Me.OlExplr_Activate
    MsgBox("Hello!")
End Sub

CodePudding user response:

Instead of statically declaring the event handler, try to use AddHandler statement dynamically at run-time.

Off the top of my head (I don't use VB.Net):

Private Sub ThisAddIn_Startup() Handles Me.Startup
     OlExplr = Application.ActiveExplorer
     AddHandler (OlExplr As ExplorerEvents).Activate, AddressOf OlExplr_Activate
End Sub

Private Sub OlExplr_Activate()
'''Do some stuff here
End Sub

CodePudding user response:

Using what Dmitry posted, I was able to tweak the syntax to capture and fire the event:

Private WithEvents OlExplrs As Outlook.Explorers
Private WithEvents Explr As Microsoft.Office.Interop.Outlook.Explorer

Private Sub ThisAddIn_Startup() Handles Me.Startup
    OlExplrs = Application.Explorers
End Sub

Private Sub OlExplr_NewExplorer(Explorer As Explorer) Handles OlExplr.NewExplorer
    If Explorer.Caption = "To-Do List..." Then
        Explr = OlExplrs.Item(Explorer.Caption)
    End If
    AddHandler Explr.Activate, AddressOf MyActivateHandler
End Sub

    Private Sub MyActivateHandler()
        '''Do stuff...
    End Sub

It turns out, however, that the way I have Outlook set up, the Activate method is not triggered when the desired explorer window (in this case, the to-do list) is brought into focus. The Activate method only fires if the explorer goes from a minimized to a maximized state or gets initialized and opened.

The Hide/Unhide event (or whatever it is that is happening when moving from the inbox window to the to-do list window and back, etc...) is really what I would need. This does not exist as a built in event for the explorer, so perhaps a custom event could work? I will open a separate question if I cannot find a solution.

  • Related