I'm developing my first Outlook 2010 add-in using C# and Visual Studio 2010. Thus far my project is going nicely. I have a custom tab in the ribbon, and it updates every time a new message is selected in the outlook window. Text from the email is analyzed and different information is displayed in the ribbon.
What makes this all work is in my ThisAddIn_Startup method. In there I bind to some of the Outlook events, so my code can react accordingly when a new email is selected.
What's really troubling is that this is failing intermittently about 1/3rd of the time. Our company has several outlook add-ins from various vendors, so it's hard to know exactly what is happening during Outlook's startup sequence. Sometimes my code binds to Outlook's events, sometimes it doesn't. When it doesn't, I close and re-open Outlook and it works. Any advice would be appreciated. Is there a better way I can make this work? If I have to expect these intermittent startup failures, any ideas on how I can make my add-in aware of this and recover without requiring a restart of the app?
Here is a sample of my code from ThisAddIn.cs:
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
// set up event handler to catch when a new explorer (message browser) is instantiated
Application.Explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(NewExplorerEventHandler);
// ...and catch any Explorers already existing before this startup event was fired
foreach (Outlook.Explorer Exp in Application.Explorers) {
NewExplorerEventHandler(Exp);
}
}
public void NewExplorerEventHandler(Microsoft.Office.Interop.Outlook.Explorer Explorer) {
if (Explorer != null) {
//set up event handler so our add-in can respond when a new item is selected in the Outlook explorer window
Explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(ExplorerSelectionChange);
}
}
private void ExplorerSelectionChange() {
Outlook.Explorer ActiveExplorer = this.Application.ActiveExplorer();
if (ActiveExplorer == null) { return; }
Outlook.Selection selection = ActiveExplorer.Selection;
Ribbon1 ThisAddInRibbon = Globals.Ribbons[ActiveExplorer].Ribbon1;
if (ThisAddInRibbon != null) {
if (selection != null && selection.Count == 1 && selection[1] is Outlook.MailItem) {
// one Mail object is selected
Outlook.MailItem selectedMail = selection[1] as Outlook.MailItem; // (collection is 1-indexed)
// tell the ribbon what our currently selected email is by setting this custom property, and run code against it
ThisAddInRibbon.CurrentEmail = selectedMail;
} else {
ThisAddInRibbon.CurrentEmail = null;
}
}
}
UPDATE: I added two variables to the ThisAddIn class for the two objects I want to catch events from:
Outlook.Explorers _explorers; // used for NewExplorerEventHandler
Outlook.Explorer _activeExplorer; // used for ExplorerSelectionChange event
In ThisAddIn_Startup:
_explorers = Application.Explorers;
_explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(NewExplorerEventHandler);
In ExplorerSelectionChange:
_activeExplorer = this.Application.ActiveExplorer();