简体   繁体   中英

How to monitor a specific folder (not the default inbox) so it fires another macro when items are added?

I created a pst file called All Inbox and have rules to move all email from all other accounts into its Inbox.

I have a macro that runs all those rules at once (RunRules) to process messages (move, print, file, etc.) from the All Inbox/Inbox folder, but I have to run it manually (I have a button on the Quick Access Toolbar).

I want to monitor the All Inbox/Inbox folder and fire my RunRules macro.

The code I've seen monitors the default folder. I don't know how to modify the code to specify the All Inbox/Inbox folder.

I also don't know where to put the options, subs, etc. All in a new module?

Option Explicit
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
    Dim objNS As Outlook.NameSpace
    Dim objWatchFolder As Outlook.Folder

    Set objNS = Application.GetNamespace("MAPI")
    Set objWatchFolder = objNS.Folder("All Inbox").Folders("Inbox")

    Set objItems = objWatchFolder.Items
End Sub
Private Sub olkFolder_ItemAdd(ByVal Item As Object)
    Dim xitem As Outlook.MailItem
    If Item.Class = olMail Then
        RunRules
    End If
End Sub

Do I specify All Inbox as a store?

Your missing S on objNS.Folder Should be objNS.Folders("All Inbox").Folders("Inbox")

Application_Startup event Should be under ThisOutlookSession class module and you could also have your RunRules sub in the same madule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM