Outlook Mail Item Open Event Doesn't Work

  • Thread starter Nenad Dobrilovic
  • Start date
N

Nenad Dobrilovic

I have an interesting issue with Open mail item's event.

I have a MailItem wrapper class, which exposes Opening event like this;

public class MailDocument
{
private Outlook.MailItem mailItem;

private static Dictionary<string, MailDocument> documents = new
Dictionary<string, MailDocument>();

public event Action<MailDocument, CancelEventArgs> Opening;

public MailDocument(Outlook.MailItem item)
{
this.mailItem = item;
((Outlook.ItemEvents_10_Event)mailItem).Open += new
Outlook.ItemEvents_10_OpenEventHandler(onOpen);
documents.Add(item.EntryID, this);
}

private void onOpen(ref bool cancel)
{
CancelEventArgs args = new CancelEventArgs(cancel);
Opening(this, args);
cancel = args.Cancel;
}

public static MailDocument Get(Outlook.MailItem mailItem)
{
MailDocument mailDocument;
documents.TryGetValue(mailItem.EntryID, out mailDocument);
return mailDocument;
}
}
}

NewInspector event handler is like this:

Outlook.MailItem current = inspector.CurrentItem as
Outlook.MailItem;
MailDocument mailDocument = MailDocument.Get(current);
mailDocument.Opening += new Action<MailDocument,
CancelEventArgs>(OnOpening);

Opening event is never invoked.
But, if I change these lines of code, event handler is invoked every time.

Outlook.MailItem current = inspector.CurrentItem as
Outlook.MailItem;
MailDocument mailDocument = new MailDocument(current);
mailDocument.Opening += new Action<MailDocument,
CancelEventArgs>(OnOpening);

Do you have an idea why this is happening? I noticed that in the default
Microsoft example is the same. Also, if I cancel a mail item default
inspector, it does shows for a part of second, so that a display is
flickering a little bit.
 
K

Ken Slovak - [MVP - Outlook]

Unless the class is static you need to call new on it to initialize it
correctly.
 
N

Nenad Dobrilovic

At the startup event of the add-in, it creates all mail items and keeps them
in a dictionary, as you can see in a code example, so that it can get a right
one when event starts.
But, even if method Get returns a mail item wrapper with the same event ID,
it's open event never fires.
Does it mean that you can attach a event handler only on a current mail item
of the Inspector object returned as a parameter of a NewInspector event?
 
K

Ken Slovak - [MVP - Outlook]

When you use the new keyword you create a new instance of your class. That's
what's required and why it works when you do use that keyword, as you
already said.

I usually handle things differently myself. I use an Inspector wrapper class
that has in it declarations for a mail item and event handlers for that mail
item and for the Inspector itself (Activate, Close, etc.). When I get
NewInspector I instantiate an instance of the wrapper class and add it to a
list to keep it alive. I set the Inspector property of the class to the new
Inspector, set the Mail property of the class to the mail item that's
Inspector.CurrentItem, and set up the event handlers including item.Open()
and so on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top