Outlook 2007 VBA - an event for when a message is moved into a folder by a rule

I'm trying to make Outlook catch messages that arrive with a particular subject line, move them to a folder, and also save the text of the body to a txt file on my c: drive (to be picked up by Excel subsequently).  I've managed to create a rule to move the messages to the folder, and am working on VBA code to do the rest.

How would I catch an event that triggers when the rule moves the message to that folder?  I will obviously need to have the message in a variable (call it Item) so that I can do the next bit.

Presumably then I use Item.Subject, and Item.Body, etc.

The folder is called InboxAccounts, and it is a subfolder of my usual Inbox folder.  I am using Outlook in a corporate environment, with Exchange Server.

Thank you!
LVL 2
jan24Asked:
Who is Participating?
 
Chris BottomleySoftware Quality Lead EngineerCommented:
The following demonstrates the method.

As you can see the msgbox triggers displaying the body of the mail item as added.   Any normal VBA activity can now be done on the mail item as appeared in the folder.

First time around either restart outlook or execute the Application_Startup sub to initialise the handler ... on startup it will auto initialise.

Chris
Option Explicit
Public WithEvents folderX As Outlook.Items

Private Sub Application_Startup()
    Set folderX = Application.Session.GetDefaultFolder(olFolderInbox).Folders("InboxAccounts").Items
End Sub

Private Sub folderX_ItemAdd(ByVal Item As Object)
'    Stop
    MsgBox Item.Body
End Sub

Open in new window

0
 
jan24Author Commented:
Works a treat.  Many thanks, Chris!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.