问题
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