View Single Post
  #2  
Old April 16th 07, 02:58 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default ItemAddEventHandler only runs once

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" wrote in message
...
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.OlDef aultFolders.olFolderInbox);
inbox.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(inboxFolde rItemAdded);
}

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

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


Ads