Handler for Outlook Explorer.SelectionChange event disappears ??

OK, I'm new to VSTO.... my registered handler pops up a message-box when I select different emails in my inbox, different folders, etc. JUST FINE.....

BUT... if I generally do stuff like send or open an email, , then when I get back to the Explorer and select stuff, the event handler no longer gets executed.

I saw this behavior with other events such as Inspector.Activate

What am I missing Here's the code:

public partial class ThisApplication

{

Outlook.Explorer thisExplorer;

private void ThisApplication_Startup(object sender, System.EventArgs e)

{

thisExplorer = this.ActiveExplorer();

thisExplorer.SelectionChange +=

new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_SelectionChangeEventHandler(ThisApplication_SelectionChange);

}

void ThisApplication_SelectionChange()

{

MessageBox.Show("Selection change");

}




Answer this question

Handler for Outlook Explorer.SelectionChange event disappears ??

  • HimanshuG

    Hello Allen,

    if you want to use different Inspectors,

    you must use the same technique.

    Wrapp the Inspectors and remember them in memory.

    greets, Helmut

    [http://www.x4u.de]



  • Adner

    Hello Allen,

    again here the 2 files:

    ThisAddIN.cs

    -----------------------------------

    /// Outlook Sample code by H.Obertanner http://www.x4u.de

    /// Generated and tested with Outlook 2007 B2TR and VSTO 2005 SE

    using System;

    using System.Windows.Forms;

    using Microsoft.VisualStudio.Tools.Applications.Runtime;

    using Outlook = Microsoft.Office.Interop.Outlook;

    using Office = Microsoft.Office.Core;

    using System.Collections;

    namespace FolderSwitchSample

    {

    /// <summary>

    /// This is the genereated VSTO Add-in class.

    /// </summary>

    public partial class ThisAddIn

    {

    /// <summary>

    /// This generic collection holds a reference to our open explorers.

    /// </summary>

    Hashtable _WrappedExplorers;

    /// <summary>

    /// This variable must be used to keep a reference to the Application Explorers collection.

    /// Required to get informed about new Explorers.

    /// </summary>

    Outlook.Explorers _Explorers;

    /// <summary>

    /// This method is called when our add-in gets loaded into Outlook.

    /// </summary>

    /// <param name="sender">The sender is the Outlook instance.</param>

    /// <param name="e">No usefull parameters.</param>

    private void ThisAddIn_Startup(object sender, System.EventArgs e)

    {

    // initialize the List that will keep an eye on our active Outlook Explorers.

    _WrappedExplorers = new Hashtable (10);

    // Do we have already some explorers after startup

    _Explorers = Application.Explorers;

    for (int i = _Explorers.Count; i >= 1; i--)

    {

    // Wrap the Explorer and do some usefull with it

    WrapExplorer(_ExplorersIdea);

    }

    // get notified of new opened Explorers

    _Explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);

    }

    /// <summary>

    /// This event occures en a new outlook explorer has been opened.

    /// </summary>

    /// <param name="Explorer">The opened Outlook Explorer object.</param>

    void Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)

    {

    // Wrap the Explorer and do some usefull with it

    Outlook.Explorer activeExplorer = Application.ActiveExplorer();

    WrapExplorer(activeExplorer);

    }

    /// <summary>

    /// This method wraps an Outlook Explorer object and registers for the ExplorerClosed event.

    /// </summary>

    /// <param name="Explorer">The Outlook Explorer Object.</param>

    void WrapExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)

    {

    // Wrap the Explorer and do some usefull with it

    ExplorerWrapper wrappedExplorer = new ExplorerWrapper(Explorer);

    // register for the closed event, so we can releas it from memory

    wrappedExplorer.ExplorerClosed += new ExplorerClosedDelegate(UnwrapExplorer);

    }

    /// <summary>

    /// This method should be called when a wrapped Outlook Explorer has been closed.

    /// </summary>

    /// <param name="explorerId">The unique ID of the closed explorer.</param>

    void UnwrapExplorer(Guid explorerId)

    {

    // if we have a reference to this explorer we can release it now

    if (_WrappedExplorers.ContainsKey (explorerId))

    {

    _WrappedExplorers.Remove(explorerId);

    }

    }

    /// <summary>

    /// This event occures when our Add-in gets unloaded from Outlook.

    /// </summary>

    /// <param name="sender">The sender is the Outlook instance.</param>

    /// <param name="e">No usefull parameters.</param>

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

    {

    // remove all references to the wrapped explorers.

    _WrappedExplorers.Clear();

    }

    #region VSTO generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InternalStartup()

    {

    this.Startup += new System.EventHandler(ThisAddIn_Startup);

    this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

    }

    #endregion

    }

    }

    -----------------------------------------------

    ExplorerWrapper.cs

    ---------------------------------------

    /// Outlook Sample code by H.Obertanner http://www.x4u.de

    /// Generated and tested with Outlook 2007 B2TR and VSTO 2005 SE

    using System;

    using System.Windows.Forms;

    using Microsoft.VisualStudio.Tools.Applications.Runtime;

    using Outlook = Microsoft.Office.Interop.Outlook;

    using Office = Microsoft.Office.Core;

    namespace FolderSwitchSample

    {

    /// <summary>

    /// A delegate used to inform the application that an explorer has been closed.

    /// </summary>

    /// <param name="explorerId">The unique ID of the Explorer that has been closed.</param>

    internal delegate void ExplorerClosedDelegate (Guid explorerId);

    /// <summary>

    /// The ExplorerWrapper is used to wrap around an Outlook Explorer.

    /// It keeps a reference to the Explorer in memory and registers for the Explorer events.

    /// </summary>

    internal class ExplorerWrapper

    {

    /// <summary>

    /// This event is fired from out wrapper when the wrapped Outlook Explorer has been closed.

    /// </summary>

    public event ExplorerClosedDelegate ExplorerClosed;

    /// <summary>

    /// An unique ID that cabe used to identify this explorer (commandbars, buttons, etc...)

    /// </summary>

    Guid _Id;

    /// <summary>

    /// An unique ID that cabe used to identify this explorer (commandbars, buttons, etc...)

    /// </summary>

    public Guid ID

    {

    get { return _Id; }

    }

    /// <summary>

    /// The Outlook Explorer object used to keep a reference in memory and register for the explorer events.

    /// </summary>

    Outlook.ExplorerClass _Explorer;

    /// <summary>

    /// The Outlook Explorer object used to keep a reference in memory and register for the explorer events.

    /// </summary>

    public Outlook.Explorer Explorer

    {

    get { return _Explorer; }

    }

    /// <summary>

    /// The construction code.

    /// </summary>

    /// <param name="explorer">The outlook explorer that should be wrapped.</param>

    public ExplorerWrapper(Outlook.Explorer explorer)

    {

    _Id = Guid.NewGuid();

    _Explorer = explorer as Outlook.ExplorerClass ;

    if (_Explorer != null)

    {

    _Explorer.FolderSwitch += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_FolderSwitchEventHandler(_Explorer_FolderSwitch);

    _Explorer.ExplorerEvents_Event_Close += new Microsoft.Office.Interop.Outlook.ExplorerEvents_CloseEventHandler(_Explorer_ExplorerEvents_Event_Close);

    }

    }

    /// <summary>

    /// This event occures when this explorer has been closed.

    /// </summary>

    void _Explorer_ExplorerEvents_Event_Close()

    {

    // hereelease areferences to the explorer from memory.

    _Explorer.FolderSwitch -= new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_FolderSwitchEventHandler(_Explorer_FolderSwitch);

    _Explorer.ExplorerEvents_Event_Close -= new Microsoft.Office.Interop.Outlook.ExplorerEvents_CloseEventHandler(_Explorer_ExplorerEvents_Event_Close);

    _Explorer = null;

    // fire an event toinform the application that this explorer has been closed.

    if (ExplorerClosed != null)

    {

    ExplorerClosed(this._Id);

    }

    }

    /// <summary>

    /// This event occures when another folder has been selected in this explorer.

    /// </summary>

    void _Explorer_FolderSwitch()

    {

    Outlook.MAPIFolder selectedFolder = _Explorer.CurrentFolder;

    MessageBox.Show(String.Format("You have selected Folder {0} in Explorer: {1}", selectedFolder.Name, _Id));

    selectedFolder = null;

    }

    }

    }

    -----------------------------------------------------

    Just past the complete code into the files thisaddin.cs and ExplorerWrapper.cs.
    Start Outlook and select different Folders.

    Everytime you select another Folder - a MessageBox should come up.

    Hope this helps,
    greets Helmut



  • Sean D Wright

    Helmut,

    Thanks for the information, I will examine your example and try a different approach.

    My goal is to have my add-in show a form when MailItems having certain attachments are opened.

    I first tried handling the inspector's Activate event, but also found that it would stop calling the handler after a while.

    Next, I tried testing the explorer's selection when the SelectionChange fires, and registering a MailItemOpen handler on the MailItem in case it gets opened, launching my form from there.

    Your site is great, I will learn a lot from it!

    Alles Gute,

    -Allen (www.razdow.com)



  • Jaime Stuardo

    Helmut,

    Thanks, I have this working now....

    I hope this will solve my problem with handlers for inspector events.

    -Allen



  • irvendeep

    Hello Allen,

    here maybe the problem is the

    this.ActiveExplorer()

    if the explorer is changed or maybe disabled, the ActiveExplorer changes and therefore you won't get any event.

    Don't know if it helps, but I have a small Explorer.FolderSwitch sample on my homepage that deals with multiple explorers and the events.

    http://www.x4u.de/Outlook/Codesnippets/tabid/61/Default.aspx


    Also I started a new "Learn VSTO with me" Project on my homepage.

    http://www.x4u.de/Outlook/Howtogetstarted/tabid/226/Default.aspx

    Hope this helps,

    greets, Helmut

    [http://www.x4u.de]



  • Mervyn-w

    Helmut,

    I've pasted your code into my thisApplciation.cs, and added your explorer wrapper to my project.

    I'm having a problem because your Application.Explorers reference doesn't work. I don't seem to have a handle on the right Application context I guess that this is precisely my problem...not having access to the top-level where the collection of explorers exists...thus my call this.ActiveExplorer()

    I realize I'm a bit confused on this!

    -Allen



  • Handler for Outlook Explorer.SelectionChange event disappears ??