Group:  Microsoft Outlook ยป microsoft.public.outlook.program_addins
Thread: ItemAddEventHandler only runs once

Geek News

ItemAddEventHandler only runs once
Dave 4/16/2007 1:24:03 PM
Hi,

Im trying to catch all incoming new mail using the code below. The problem
is that it is only run once, so when i get 3 new messages, only the first
mail will be used in the eventhandler.

--


private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.MAPIFolder inbox =
Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
inbox.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(inboxFolderItemAdded);
}

public void inboxFolderItemAdded(object item)
{
if (item is Outlook.MailItem)
{
Outlook.MailItem mail = (Outlook.MailItem)item;

MessageBox.Show(mail.Subject.ToString());
}
}

Re: ItemAddEventHandler only runs once
"Ken Slovak - [MVP - Outlook]" <kenslovak[ at ]mvps.org> 4/16/2007 1:58:44 PM
Declare the inbox object at class level and not in ThisAddIn_Startup and I'd
recommend also declaring an object for inbox.Items and attaching the event
handler to that instead of to inbox.Items as you are doing.

The way that object is declared it's going out of scope and is being garbage
collected.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Dave" <Dave[ at ]discussions.microsoft.com> wrote in message
news:A14881A7-81F7-4CED-BA8F-DD86F290D0CA[ at ]microsoft.com...
[Quoted Text]
> Hi,
>
> Im trying to catch all incoming new mail using the code below. The problem
> is that it is only run once, so when i get 3 new messages, only the first
> mail will be used in the eventhandler.
>
> --
>
>
> private void ThisAddIn_Startup(object sender, System.EventArgs e)
> {
> Outlook.MAPIFolder inbox =
> Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
> inbox.Items.ItemAdd += new
> Outlook.ItemsEvents_ItemAddEventHandler(inboxFolderItemAdded);
> }
>
> public void inboxFolderItemAdded(object item)
> {
> if (item is Outlook.MailItem)
> {
> Outlook.MailItem mail = (Outlook.MailItem)item;
>
> MessageBox.Show(mail.Subject.ToString());
> }
> }
>

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