3

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?

magic_turtle
  • 1,243
  • 3
  • 17
  • 37
  • ...Perfect question which is completely not a duplicate of the marked duplicate. Triggering events on subfolders is a sub question of the duplicate marked, not a duplicate. It is more precise. I wish the community would be more forgiving in this regard. – FreeSoftwareServers Sep 27 '19 at 09:11

2 Answers2

3

Since you did not mention

Private WithEvents olInboxItems As Items

You probably forgot

Private WithEvents subfolderItems As Items
niton
  • 8,771
  • 21
  • 32
  • 52
1

Make sure you declare subfolderItems variable on the global level to make sure it stays alive.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78