问题
I have the requirement to capture the event when the mail is moved from Subfolder to Inbox
The folder structure is as below
myarchive-mailbox name
Inbox Main folder
requests Sub folder
myarchive
Inbox
requests
When the email is moved from requests subfolder to Inbox, of myarchive mailbox Name ,this mailbox item should be captured and the event handler should be invoked.
I have already implemented the code for capturing the event when the file is moved from myarchive inbox to requests.The code I have written is as below
Private WithEvents Items As Outlook.Events
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objFolder As Outlook.MAPIFolder
Dim objNs As Outlook.NameSpace
Set olApp =Outlook.Application
Set objNS =olApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders("myarchive")
Set objFolder=objFolder.Folders("Inbox")
Set Items=objFolder. Folders("requests").Items
End Sub
Private Sub Items_ItemsAdd(ByVal item As Object)
MsgBox "You moved the mail to requests folder"
End Sub
回答1:
The Folder
object has a BeforeItemMove
event. In the ThisOutlookSession module, declare a folder object WithEvents
to expose its events.
Private WithEvents mArchReqs As Folder
Public Property Set ArchReqs(olFldr As Folder)
Set mArchReqs = olFldr
End Property
Public Property Get ArchReqs() As Folder
Set ArchReqs = mArchReqs
End Property
Next you have set the folder you want to watch. Here I set the folder when the application starts up.
Private Sub Application_Startup()
Set Me.ArchReqs = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("requests")
End Sub
Finally, you can write the BeforeItemMove
event procedure.
Private Sub mArchReqs_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
Debug.Print Item.Subject
Debug.Print MoveTo.Name
End Sub
回答2:
Assuming you are moving it to main Default Inbox, Then try the following code
Dim WithEvents SubFolder As Outlook.Folder
Dim Inbox As Outlook.Folder
Dim olNs As Outlook.NameSpace
Private Sub Application_Startup()
Set olNs = Application.GetNamespace("MAPI")
Set SubFolder = olNs.Folders("myarchive").Folders("Inbox").Folders("requests")
Set Inbox = Application.Session.GetDefaultFolder(olFolderInbox)
End Sub
Private Sub SubFolder_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
If MoveTo = Inbox Then
MsgBox Item.Subject & " was moved to Inbox"
End If
End Sub
Else change this line
Set Inbox = Application.Session.GetDefaultFolder(olFolderInbox)
To This
Set Inbox = olNs.Folders("myarchive").Folders("Inbox")
Folder.BeforeItemMove Event
来源:https://stackoverflow.com/questions/38577111/capture-the-event-while-moving-the-mail-from-subfolder-to-inbox