Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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!
1 Solution
Chris BottomleyCommented:
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

jan24Author Commented:
Works a treat.  Many thanks, Chris!

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now