Outlook ItemAdd event on IMAP folder fires only when folder is selected

outlook, vba

Solution

I Was struggling with a similar issue to move mail-items after they were sent and used your code to perform this task (thx!). There were several issues which still had to be resolved.

First of all, the items were moved, but immediately after they were placed into the trash folder. This seems to be an IMAP issue (Gmail) and may be resolved by changing the Internet E-mail Settings of the mailbox account from "Move deleted items to the following folder on the server" to "Mark items for deletion but do not move them automatically".

The second challenge was, like yours, trigger the code to do its work. In the account configuration the save sent emails option is disabled (since this is automatically performed by the Gmail server). I needed to sync the Sent items (MAPI) folder with the Send items (IMAP) folder. I achieved this by configuring the "send/receive" groups for this email account (in the group All account) and selecting the Sent items folder.

Now this folder is synced without the necessity to open the folder for syncing. I hope that this will also resolve your issue.

Peter

Problem

I'm running in a little problem with Outlook VBA programming, and would like to know if there's a solution, or if this is just another "known issue". Context: I have configured an Outlook e-mail account to access my web email provider through IMAP. In Outlook, I can properly see my web email folders. My provider's spam filter moves spam messages into the Spam folder. I would like to automatically move messages that get put into the Spam folder into another folder, in my local pst file. I have it working 99% (through the code provided below for reference). Issue: I can see that there are messages in the Spam folder (there is a bold unread message count beside the folder name), but the ItemAdd even will only fire when I click on the folder. At that point, I see the contents of the spam folder, and then see all of the new spam being moved to my local folder. Is there another trigger source beside ItemAdd I could use for running my code without having to click on the folder? Is there an event that gets triggered when the unread count for a folder changes? Technical details: - Windows 8 OS - Using Outlook 2002 (Yes, I know...) - I'm an experienced C/C++ developer, but minimal experience in VBA, and none with Outlook. VBA code: ``` Public WithEvents myItems As Outlook.Items Public Sub Application_Startup() Dim myNameSpace As Outlook.NameSpace Const mailboxName As String = "Mail.com" Const subfolderName As String = "Spam" ' Reference the items in the MAPI spam folder ' Because myOlItems is declared "WithEvents" the ItemAdd event will fire below. Set myNameSpace = Application.GetNamespace("MAPI") On Error GoTo noSpamFolder Set myItems = myNameSpace.Folders(mailboxName).Folders(subfolderName).Items On Error GoTo 0 Exit Sub noSpamFolder: MsgBox "Unable to find folder <" & mailboxName & "/" & subfolderName & ">" End Sub Private Sub myItems_ItemAdd(ByVal Item As Object) Dim suspectFolder As Outlook.MAPIFolder ' Check to make sure it is an Outlook mail message, otherwise ' subsequent code will probably fail depending on what type ' of item it is. If TypeName(Item) = "MailItem" Then ' Move message to the 'suspect' folder On Error GoTo noSuspectFolder Set suspectFolder = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("suspect") On Error GoTo 0 Item.Move suspectFolder End If Exit Sub noSuspectFolder: MsgBox "Unable to find folder <suspect> as a sub-folder of default inbox folder" End Sub ```

Original source