vbaoutlook

How to apply ItemAdd event to custom folder? Outlook 2010 VBA


I am trying to use ItemAdd event method to monitor when new items were added to my subfolder under Inbox.

My dir structure is like this:

- Inbox 
- - subfolder

I want to run the code when new email is added to a subfolder (the reason is I am using a rule that redirects certain emails to this subfolder).

Currently I am monitoring my Inbox using this:

Private Sub Application_Startup()
   Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).items
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
/* Code here */ 
End Sub 

but when I try to do it for a subfolder

Set subfolderItems = Session.GetDefaultFolder(olFolderInbox).Folders("subfolder").items 
Private Sub subfolderItems_ItemAdd(ByVal Item As Object) 

it doesn't work. Why? How can I capture when new email is added?


Solution

  • Since you did not mention

    Private WithEvents olInboxItems As Items
    

    You probably forgot

    Private WithEvents subfolderItems As Items