An ItemAdd macro watches a specific folder and runs a macro on it as messages are dropped into it, using any method: using rules, dragging messages to the folder, using the move or copy to folder command, or even copy and paste in place.
You can watch any folder that is in your profile using ItemAdd, including share folders; you can watch more than one folder, but you need to identify each folder in the Application_Startup macro.
When you use an Application_Startup macro, you set a variable that Outlook remembers while it's open (one exception: if any macro fails, the variable will be forgotten).
If you need to watch more than one folder, you'll need to use declare another variable name (Private WithEvents... ) and set it in the Application_Startup macro. Each folder will have it's own variable.
Option Explicit Private objNS As Outlook.NameSpace Private WithEvents objItems As Outlook.Items Private Sub Application_Startup() Dim objWatchFolder As Outlook.Folder Set objNS = Application.GetNamespace("MAPI") 'Set the folder and items to watch: Set objWatchFolder = objNS.GetDefaultFolder(olFolderInbox) Set objItems = objWatchFolder.Items Set objWatchFolder = Nothing End Sub
Along with telling Outlook which folder(s) to watch, you need the ItemAdd macro that tells Outlook what do when a new item is dropped in the folder. You'll do this with a macro that has the variable name in it's name: Private Sub objItems.ItemAdd(ByVal Item As Object). If you are watching more than one folder, each folder will have it's own macro.
When the ItemAdd macro fires, you'll refer to the items using the variable set in parenthesis. In this example, it's Item: (ByVal Item As Object).
Private Sub objItems_ItemAdd(ByVal Item As Object) ' Your code goes here MsgBox "Message subject: " & Item.Subject & vbcrlf & "Message sender: " & Item.SenderName &" (" & Item.SenderEmailAddress & ")" Set Item = Nothing End Sub
All of the code used in an ItemAdd macro goes in ThisOutlookSession.
To test your macro without restarting Outlook, place the cursor in the Application_Startup macro and click Run. To test the ItemAdd macro without sending yourself mail, you can select an item, copy and paste in place (Ctrl+C, V). Alternately, you can drag an item from another folder and drop it in the folder you're watching. Eitehr method will trigger the ItemAdd macro.
Watch other folders
You can watch any folder in your profile. The folders in your default data file will use GetDefaultFolder method, including folders you added. If you're watching a folder you added, use this format:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Folder name")
If the folder is in another data file, you'll need to use the GetFolderPath function on "this page" and reference it using the display name in the navigation pane: GetFolderPath("firstname.lastname@example.org\My Stuff").
Shared Exchange mailboxes will use the GetSharedDefaultFolder method.
See "Working with VBA and non-default Outlook Folders" for help referencing other folders in a macro. There is a list of default folder references on that page, along with the GetFolderPath function and code that uses the GetSharedDefaultFolder method.
If you need to watch two (or more) folders and run the same macro on each, you'll need to set each folder name variable and create an ItemAdd macro. If the macro is short, duplicating the code isn't a big deal, but you can call another macro instead of repeating the same code multiple times.
In this example, I'm watching two folders (set in the Application Startup macro, not shown here) and when a new item arrives, it passes the Item object to another macro to do something.
Private Sub slipstickItems_ItemAdd(ByVal Item As Object) On Error Resume Next SetCategories Item End Sub Private Sub cdoliveInbox_ItemAdd(ByVal Item As Object) On Error Resume Next SetCategories Item End Sub Private Sub SetCategories(ByVal Item As Object) ' do something End if
- Create Appointment From Email Automatically
- How to Change the Font used for Outlook's RSS Feeds
- How to Process Mail After Business Hours
- Process messages received on a day of the week
- Processing Incoming E-mails with Macros
- Save all incoming messages to the hard drive
- Use VBA to move messages with attachments