Twitter
Powered By
Powered by Squarespace
Navigation
« Connecting an iOS device to a mixed 802.11b/n WiFi network | Main | Starcraft II Patch Download Problem »
Monday
Jan172011

Event Handlers only firing once in Microsoft Office AddIns

I've just been working on a project where we were to create some AddIns for several versions of Microsoft Office. Now I knew there was a lot of bad blood around Office AddIns but thought they were being overblown as I finished off the 2010 AddIn without so much as a hiccup. The 2007 and 2003 AddIns however showed why Office has the reputation it has.

The problem I ran into was that I had to have several event handlers to catch two events. The opening of a new inspector and a simple button click. So I did what you'd expect to do and registered them in the startup methods.

Initial testing went fine as I started up Outlook and triggered one event, made some changes, restarted it and then tested the other event. It took a while until I tried to test both events following one another at which point I found only one would trigger and then both would even handler hooks would be forgotten and wouldn't rehook in until a restart of the application.

public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Outlook.Explorer explorer = this.Application.ActiveExplorer();
        Outlook.Application app = (Outlook.Application)explorer.Application;

        app.NewInspector += new InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }
}

After much searching I began to come across implications that the garbage collector was removing the references after the first event. I was at a loss at what to do until I came across another discussion where someone was having a similar problem and the response was to save the object in a class level variable to avoid the garbage collector from removing it.

A quick edit and some testing showed this to work reliably. So, if Office is only triggering an event once make sure there object references are stored somewhere the garbage collector won't go. And make sure to assign the object before you register the handler or the garbage collector will still find it.


public partial class ThisAddIn
{
    public Inspectors _appInspectors;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Outlook.Explorer explorer = this.Application.ActiveExplorer();
        Outlook.Application app = (Outlook.Application)explorer.Application;

        _appInspectors = app.Inspectors;
        _appInspectors.NewInspector += new InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }
}

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (9)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: GFI Norte
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG
  • Response
    Response: więcej
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG
  • Response
    Response: sprawdź nas
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG
  • Response
    Response: sprawdź nas
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG
  • Response
    Response: tania dieta
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG
  • Response
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG
  • Response
    Response: gtVrJMGd
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG
  • Response
    Response: ugg broome ii
    Before I'm advised they almost certainly had been offered something which they didn't want, allows examine the motivation for getting. In case you had been taking a look at purchasing software package to run on the Windows primarily based system perhaps it will be valuable to make certain that it labored ...
  • Response
    Response: removing hair
    Event Handlers only firing once in Microsoft Office AddIns - Thoughts - TFG

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>