Outlook Banter

Outlook Banter (http://www.outlookbanter.com/)
-   Add-ins for Outlook (http://www.outlookbanter.com/add-ins-outlook/)
-   -   Outlook Mail Item Open Event Doesn't Work (http://www.outlookbanter.com/add-ins-outlook/79851-outlook-mail-item-open-event.html)

Nenad Dobrilovic October 7th 08 04:30 PM

Outlook Mail Item Open Event Doesn't Work
 
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 Dictionarystring, MailDocument documents = new
Dictionarystring, MailDocument();

public event ActionMailDocument, 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 ActionMailDocument,
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 ActionMailDocument,
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

Ken Slovak - [MVP - Outlook] October 7th 08 06:24 PM

Outlook Mail Item Open Event Doesn't Work
 
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" wrote in
message ...
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 Dictionarystring, MailDocument documents = new
Dictionarystring, MailDocument();

public event ActionMailDocument, 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 ActionMailDocument,
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 ActionMailDocument,
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



Nenad Dobrilovic October 8th 08 09:24 AM

Outlook Mail Item Open Event Doesn't Work
 
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:

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" wrote in
message ...
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 Dictionarystring, MailDocument documents = new
Dictionarystring, MailDocument();

public event ActionMailDocument, 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 ActionMailDocument,
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 ActionMailDocument,
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




Ken Slovak - [MVP - Outlook] October 8th 08 02:28 PM

Outlook Mail Item Open Event Doesn't Work
 
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" wrote in
message ...
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




All times are GMT +1. The time now is 07:50 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2006 OutlookBanter.com