Group:  Microsoft Outlook ยป microsoft.public.outlook.program_addins
Thread: Outlook Mail Item Open Event Doesn't Work

Geek News

Outlook Mail Item Open Event Doesn't Work
Nenad Dobrilovic 10/7/2008 3:30:08 PM
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.
--
Nenad Dobrilovic
Re: Outlook Mail Item Open Event Doesn't Work
"Ken Slovak - [MVP - Outlook]" <kenslovak[ at ]mvps.org> 10/7/2008 5:24:45 PM
Unless the class is static you need to call new on it to initialize it
correctly.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nenad Dobrilovic" <NenadDobrilovic[ at ]discussions.microsoft.com> wrote in
message news:47393D7F-DA2B-4D0B-B1DA-43ABBD65EAD7[ at ]microsoft.com...
[Quoted Text]
>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.
> --
> Nenad Dobrilovic

Re: Outlook Mail Item Open Event Doesn't Work
Nenad Dobrilovic 10/8/2008 8:24:01 AM
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?
--
Nenad Dobrilovic


"Ken Slovak - [MVP - Outlook]" wrote:

[Quoted Text]
> Unless the class is static you need to call new on it to initialize it
> correctly.
>
> --
> Ken Slovak
> [MVP - Outlook]
> http://www.slovaktech.com
> Author: Professional Programming Outlook 2007.
> Reminder Manager, Extended Reminders, Attachment Options.
> http://www.slovaktech.com/products.htm
>
>
> "Nenad Dobrilovic" <NenadDobrilovic[ at ]discussions.microsoft.com> wrote in
> message news:47393D7F-DA2B-4D0B-B1DA-43ABBD65EAD7[ at ]microsoft.com...
> >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.
> > --
> > Nenad Dobrilovic
>
>
Re: Outlook Mail Item Open Event Doesn't Work
"Ken Slovak - [MVP - Outlook]" <kenslovak[ at ]mvps.org> 10/8/2008 1:28:49 PM
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nenad Dobrilovic" <NenadDobrilovic[ at ]discussions.microsoft.com> wrote in
message news:9EB31392-C32C-4A44-8E2E-24663564D261[ at ]microsoft.com...
[Quoted Text]
> 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?
> --
> Nenad Dobrilovic

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net