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

Posted on 2011-10-14
Last Modified: 2012-05-12
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!
Question by:jan24
    LVL 59

    Accepted Solution

    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.

    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

    LVL 2

    Author Closing Comment

    Works a treat.  Many thanks, Chris!

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Sorry for my English and Italian screenshots..... Problem: Every Year the number of E-Mails grow dramatically and the space that they take on Exchange Servers or .PST files, grow much more faster, once it was only text, now our E-Mail Archives …
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now