How to Monitor Multiple Folders?

风格不统一 提交于 2021-02-11 12:29:13

问题


I have code in ThisOutlookSession to do something with items I send. It checks the Sent Items folder. It works only for one specified mailbox/folder.

I would like to monitor three mailboxes

I can change the line:

Set Items = AInbox.Items

and it will work, but only for the mailbox I set it to.

Private Sub Application_Startup()
    Dim olNs As Outlook.NameSpace
    Dim AInbox  As Outlook.MAPIFolder
    Dim BInbox  As Outlook.MAPIFolder
    Dim CInbox  As Outlook.MAPIFolder

    Dim AItems As Items
    Dim BItems As Items
    Dim CItems As Items

    Set olNs = Application.GetNamespace("MAPI")

    Set AInbox = GetFolder("a@email.co.ukInbox\Sent Items")
    Set BInbox = GetFolder("b@email.com\Inbox\Sent Items")
    Set CInbox = GetFolder("c@email.com\Inbox\Sent Items")

    Set Items = AInbox.Items
End Sub
     

Public Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        'Do something
    End If
End Sub

回答1:


You may monitor any number of folders.

Private WithEvents AItems As Items
Private WithEvents BItems As Items
Private WithEvents CItems As Items

then

Set AItems = AInbox.Items
Set BItems = BInbox.Items
Set CItems = BInbox.Items

then

Public Sub AItems_ItemAdd(ByVal Item As Object)
Public Sub BItems_ItemAdd(ByVal Item As Object)
Public Sub CItems_ItemAdd(ByVal Item As Object)


来源:https://stackoverflow.com/questions/65969266/how-to-monitor-multiple-folders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!