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

I am having a little problem with Outlook VBA programming and would like to know if there is a solution or if this is just another "known issue".


Context

I have set up an Outlook email account to access my web email provider via IMAP. In Outlook, I can see my email folders correctly. My provider's spam filter moves spam messages to the Spam folder.

I would like to automatically move messages that end up in the Spam folder to another folder in my local pst file.

I'm 99% working (via the code below for reference).


Question

I see there are messages in the Spam folder (there is a highlighted number of unread messages next to the folder name), but ItemAdd even only fires when that folder is clicked. At this point, I see the contents of the spam folder, and then I see that all new spam is being moved to my local folder.

Is there another trigger source next to ItemAdd that I could use to run my code without having to click on the folder? Is there an event that fires when the unread invoice changes for a folder?


Specifications :

  • Windows 8
  • Using Outlook 2002 (yes, I know ...)
  • I am an experienced C / C ++ developer but minimal experience with VBA and neither Outlook nor 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

      

+3


source to share


2 answers


I was struggling with a similar issue to move post items after they were sent and used your code to accomplish this task (thanks!). There were a few more problems that still needed to be solved.

First of all, the items were moved, but immediately after they were placed in the trash folder. This appears to be an IMAP (Gmail) issue and can be resolved by changing the email settings in the email of the mailbox account from "Move deleted items to the next folder on the server" to "Mark items for deletion but do not move them automatically ".

The second problem was, like yours, getting the code to do its job. In the account configuration, the Sent Emails option is disabled (as this is automatically done by the Gmail server). I needed to synchronize the Sent Items (MAPI) folder with the Send Items (IMAP) folder. I achieved this by setting up send / receive groups for this email account (under the Whole Account group) and selecting the Sent Items folder


...

This folder now syncs without having to open the sync folder. I hope this also solves your problem.

Peter

+2


source


This makes sense - the IMAP provider in Outlook syncs a folder only when it is selected or connected through the Outlook object model.
I don't think you can do so little to sample the folder periodically (and free the MAPIFolder object between hits)



+1


source







All Articles