Outlook folder events: linking folder event to handler for each subfolder.
through use of GetDefaultFolder(olFolderInbox) to obtain the mapifolder object and casting to Outlook.MAPIFolderEvents_12_Event . a Delete event handler can be linked:
AddHandler inbox.BeforeItemMove, AddressOf ThisAddIn_BeforeItemMove
Public Sub ThisAddIn_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As Microsoft.Office.Interop.Outlook.MAPIFolder, ByRef Cancel As Boolean)
Dim d = MsgBox("Are you sure you want to delete?", MsgBoxStyle.YesNo)
End Sub
but it only works on the folder, not subfolders of the same item type.
I added each subfolder to a list of MAPIFolderEvents_12_Event's then added the event handler to each subfolder (Double checked that all folders were in the list)For Each fldr In evtList
AddHandler fldr.BeforeItemMove, AddressOf ThisAddIn_BeforeItemMove
NextYet only the inbox executes the handler?
Ideas?
Dan
Answers
Hello Dan,
BeforeItemMove event is designed to just work on one folder not including sub folders. According to your code, in for each block, it just adds the event handler to the last sub folder. So in your side, it only fire on the last sub folder in Inbox. I used code like this,
Dim folder As Outlook.Folder
Dim subfolder As Outlook.Folder
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
folder = TryCast(Me.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox), Outlook.Folder)
AddHandler folder.BeforeItemMove, AddressOf folder_BeforeItemMove
For Each subfolder1 As Outlook.Folder In folder.Folders
subfolder = subfolder1
AddHandler subfolder.BeforeItemMove, AddressOf folder_BeforeItemMove
Next
End Sub
In my side, Inbox folder has two subfolders folder 1 and folder 2. When running this code in debug mode, it works just on folder 2.
As Cindy said in this thread, since only an event listener as folder in the code is defined as an application-level, the event will fire every time. When defining these event listeners, we are not able to know the count. So here, I think the only way is to know the count of sub folders, and then define it individually.
Best regards,
Bessie
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byBessie ZhaoMSFT, ModeratorMonday, November 09, 2009 9:48 AM
All Replies
Hello Dan,
BeforeItemMove event is designed to just work on one folder not including sub folders. According to your code, in for each block, it just adds the event handler to the last sub folder. So in your side, it only fire on the last sub folder in Inbox. I used code like this,
Dim folder As Outlook.Folder
Dim subfolder As Outlook.Folder
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
folder = TryCast(Me.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox), Outlook.Folder)
AddHandler folder.BeforeItemMove, AddressOf folder_BeforeItemMove
For Each subfolder1 As Outlook.Folder In folder.Folders
subfolder = subfolder1
AddHandler subfolder.BeforeItemMove, AddressOf folder_BeforeItemMove
Next
End Sub
In my side, Inbox folder has two subfolders folder 1 and folder 2. When running this code in debug mode, it works just on folder 2.
As Cindy said in this thread, since only an event listener as folder in the code is defined as an application-level, the event will fire every time. When defining these event listeners, we are not able to know the count. So here, I think the only way is to know the count of sub folders, and then define it individually.
Best regards,
Bessie
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byBessie ZhaoMSFT, ModeratorMonday, November 09, 2009 9:48 AM


