vbaoutlook

How to apply ItemAdd event to a subfolder


I am trying to use ItemAdd event to monitor when new items are added to a subfolder under Inbox.

My directory structure is like this:

- Inbox 
- - subfolder

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

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 

I tried this for a subfolder, but this doesn't work:

Set subfolderItems = Session.GetDefaultFolder(olFolderInbox).Folders("subfolder").items

Private Sub subfolderItems_ItemAdd(ByVal Item As Object) 

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