Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook 2010 Com addin - NewExplorer never fires

Tags:

c#

outlook

add-in

For some reason in my app my FolderSwitch works on the main Explorer that opens with the application but the NewExplorer event never fires, so obviously the FolderSwitch event won't fire on a new Explorer.

I can't work out why the event doesn't fire.

private List<_Outlook.Explorer> ListOfExplorerWindows = new List<_Outlook.Explorer> { };
private _Outlook.Application Application;

public void OnConnection(object Application, Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
    this.Application = (_Outlook.Application)Application;
}

public void OnStartupComplete(ref Array custom)
{
    _Outlook.Explorer Explorer = this.Application.ActiveExplorer();
    Explorer.FolderSwitch += new _Outlook.ExplorerEvents_10_FolderSwitchEventHandler(Explorer_FolderSwitch);
    ListOfExplorerWindows.Add(Explorer);

    this.Application.Explorers.NewExplorer += new _Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
}

private void Explorers_NewExplorer(_Outlook.Explorer Explorer)
{
    Explorer.FolderSwitch += new _Outlook.ExplorerEvents_10_FolderSwitchEventHandler(Explorer_FolderSwitch);
    ListOfExplorerWindows.Add(Explorer);
}
like image 636
Matt Avatar asked Mar 02 '12 17:03

Matt


1 Answers

For any events you want to keep around when using VSTO, you are required to keep around a class-level member (Explorer, Application, Inspector, CommandBar, etc.) to keep the GC Thread from removing them. This is a resource optimization, but can also be a painful lesson to learn.

See related MSDN Forum post regarding event lifetime or similar SO post.

like image 99
SliverNinja - MSFT Avatar answered Sep 30 '22 18:09

SliverNinja - MSFT