AmazingRealist
AmazingRealist

Reputation: 45

ItemAdd event not firing for Outlook 2016 Add-in

I'm working on an old Outlook add-in that doesn't seem to trigger on the ItemAdd event

This is the code right now:

public partial class MyAddin
{
    private MAPIFolder sentItemsFolder;

    private void InternalStartup()
    {
        this.Startup += this.MyAddinStartup;
        this.Shutdown += this.MyAddinShutdown;
    }

    private void InboxFolderItemAdded(object item)
    {
        // Code that doesn't get executed
    }

    private void MyAddinShutdown(object sender, EventArgs e)
    {
        this.sentItemsFolder.Items.ItemAdd -= this.InboxFolderItemAdded;
        this.sentItemsFolder.ReleaseComObject();
        this.sentItemsFolder = null;
    }

    private void MyAddinStartup(object sender, EventArgs e)
    {
        using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
        {
            this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
            this.sentItemsFolder.Items.ItemAdd += this.InboxFolderItemAdded;
        }
    }
}

When placing a breakpoint in InboxFolderItemAdded() the breakpoint is never reached. I've come far enough to state that there seems to be some kind of incompatibility with another add-in since the code works when that add-in is disabled. The problem right now is that i don't know enough to troubleshoot what might be the issue, is is possible to flag the SentItemsFolder object as shared in any way? In case the other add-in is trying to take exclusive access (don't really know how this works). I do not have the source code for the add-in that's breaking the code.

I guess the real question is: How do i debug incompatibility issues with other add-ins, or is there some way i can edit the code i have to get around it?

Upvotes: 0

Views: 255

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

this.sentItemsFolder.Items.ItemAdd -= this.InboxFolderItemAdded;

Define the source object at the class level:

Outlook.Items items = null;

Then in the method you could set it up and subscribe to the event(s):

items = this.sentItemsFolder.Items;
items.ItemAdd -= this.InboxFolderItemAdded;

So, basically you need to declare the source Items object at the class level to prevent it from being swiped by the GC.

The same applies to the loop where you are subscribing to the Items events:

    private void MyAddinStartup(object sender, EventArgs e)
    {
        using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
        {
            this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
            this.sentItemsFolder.Items.ItemAdd += this.InboxFolderItemAdded;
        }
    }

You may define a list of Outlook.Items objects where you could keep the source objects:

List<Outlook.Items> items = new List<Outlook.Items>();

private void MyAddinStartup(object sender, EventArgs e)
{
    using (var ns = this.Application.GetNamespace("MAPI").WithComCleanup())
    {
        this.sentItemsFolder = ns.Resource.GetDefaultFolder(OlDefaultFolder.oldFolderSentMail);
        Outlook.Items itemsObject = this.sentItemsFolder.Items;        
        itemsObject.ItemAdd += this.InboxFolderItemAdded;
        // add to the list to prevent GC from swiping it out
        items.Add(itemsObject);
    }
}

Upvotes: 2

Related Questions