none
Outlook VBA code to capture deletion event for multiple selected calendar items RRS feed

  • Question

  • I am using the myFolder_BeforeItemMove event handler to capture calendar item deletes. This works well for single items but if I select multiple items and then delete, this handler fires just once. Have also tried the myItem_BeforeDelete event handler but looks like these handlers are meant for single item deletes!

    Also, when I copy and paste calendar items from one folder to myFolder the myItems_ItemAdd event doesn't trigger. Is there any other event handler which can capture a copy-paste?

    Can anyone help?

    Thursday, March 29, 2012 9:29 PM

Answers

  • BeforeItemMove should fire for each item!  You could use Item.BeforeDelete, but you'd need to monitor Explorer.SelectionChange (and Inspectors.Add for opened items) and manage a collection of objects to trap events for each one individually.

    Also, ItemAdd is not guaranteed to fire if large number of items are added to the folder at once.  If it doesn't trigger at all for you ever, then something is wrong with your code.

    For copy and paste, see the Explorer.BeforeItemCopy/Cut/Paste events.

    The best approach for monitoring folder changes is to use timers to monitor the before and after states of the Items collection in every folder you want to trap.  If this is an Exchange-only solution, I recommend using Redemption's RDOFolderSynchronizer object.


    Eric Legault
    MVP (Outlook)
    About me...

    Friday, March 30, 2012 2:44 PM
    Moderator
  • First, definitely always loop through a collection in reverse when you are removing items.

    What do you mean by "deletes do not reflect at the destination"?  Are you saying that some items aren't getting handled in BeforeItemMove but they are indeed moved to the destination folder?

    If your intent is to always trap items added to a folder, I suggest using ItemAdd in conjunction with a timer or a managed collection. For instance, when the event fires, capture the EntryIDs for every message in the folder, including the one just added.  The next time the event fires, capture all EntryIDs again and check to see if you've processed them all; ones that can't be cross-referenced have been added but didn't trigger the event.


    Eric Legault
    MVP (Outlook)
    About me...

    • Marked as answer by SharitaM Thursday, April 5, 2012 7:35 AM
    Monday, April 2, 2012 9:24 PM
    Moderator
  • I said this before and I'll say it again: if this is an Exchange-only solution, I recommend using Redemption'sRDOFolderSynchronizer object.  I've used it in a few solutions and it saves a lot of effort.  Otherwise, you'll have to do before/after comparisons of all the EntryIDs for every item in the folder to effectively determine what has been removed.  Pick a useful event to trigger this analysis, or use a timer.

    Eric Legault
    MVP (Outlook)
    About me...

    Monday, April 2, 2012 10:45 PM
    Moderator

All replies

  • BeforeItemMove should fire for each item!  You could use Item.BeforeDelete, but you'd need to monitor Explorer.SelectionChange (and Inspectors.Add for opened items) and manage a collection of objects to trap events for each one individually.

    Also, ItemAdd is not guaranteed to fire if large number of items are added to the folder at once.  If it doesn't trigger at all for you ever, then something is wrong with your code.

    For copy and paste, see the Explorer.BeforeItemCopy/Cut/Paste events.

    The best approach for monitoring folder changes is to use timers to monitor the before and after states of the Items collection in every folder you want to trap.  If this is an Exchange-only solution, I recommend using Redemption's RDOFolderSynchronizer object.


    Eric Legault
    MVP (Outlook)
    About me...

    Friday, March 30, 2012 2:44 PM
    Moderator
  • BeforeItemMove is firing for each item, and items are getting deleted at the destination folder for 3-4 items. However, when I try deleting around 200 items at once the deletes do not reflect at the destination. Have also tried deleting at the destination by stepping back in reverse (as suggested on MSDN) through the Items collection but no change.

    Any suggestions?

    Monday, April 2, 2012 9:17 PM
  • First, definitely always loop through a collection in reverse when you are removing items.

    What do you mean by "deletes do not reflect at the destination"?  Are you saying that some items aren't getting handled in BeforeItemMove but they are indeed moved to the destination folder?

    If your intent is to always trap items added to a folder, I suggest using ItemAdd in conjunction with a timer or a managed collection. For instance, when the event fires, capture the EntryIDs for every message in the folder, including the one just added.  The next time the event fires, capture all EntryIDs again and check to see if you've processed them all; ones that can't be cross-referenced have been added but didn't trigger the event.


    Eric Legault
    MVP (Outlook)
    About me...

    • Marked as answer by SharitaM Thursday, April 5, 2012 7:35 AM
    Monday, April 2, 2012 9:24 PM
    Moderator
  • I am using the BeforeItemMove event to monitor item deletes in the source folder. Then by comparing a GUID custom field/user property that I added to both the source and destination items, I am then trying to sync the deletes to the destination folder as well (through a For Loop and an If clause for each item). Works for a couple of items but when I increase the number of items to be deleted, the deletes do not reflect at the destination.

    Monday, April 2, 2012 9:35 PM
  • What I don't understand is how deleted items are supposed to reflect at the destination; you are moving items from the source to the destination, so the items is effectively deleted from the source, not the destination.  What am I missing here?


    Eric Legault
    MVP (Outlook)
    About me...

    Monday, April 2, 2012 9:58 PM
    Moderator
  • Let me try explaining it again - 

    I have 2 calendar folders and the requirement is to synchronize items deleted at the source to the destination. So, all items deleted from the source should get deleted from the destination as well. Code below:

    Private Sub olCalendarFolder_BeforeItemMove(ByVal item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
                Dim myPropertyDest, myPropertySrc As Outlook.UserProperty
                Dim oItems, oItem As Object
                Dim UIDSrc, UIDDest As String
                Dim count, inc As Integer
                Set FolderDest = NameSpaceMine.Folders("SharePoint Lists").Folders("Atripla - AtriplaTRAX Agenda")
                Set oItems = FolderDest.Items
                count = oItems.count
                
                Set myPropertySrc = item.UserProperties.Find("UniqueID")
                If TypeName(myPropertySrc) <> "Nothing" Then
                    UIDSrc = myPropertySrc.Value
                    For inc = count To 1 Step -1
                        Set oItem = oItems(inc)
                        Set myPropertyDest = oItem.UserProperties.Find("uniqueID")
                        If TypeName(myPropertyDest) <> "Nothing" Then
                            UIDDest = myPropertyDest.Value
                            If UIDSrc = UIDDest Then
                                oItem.Delete
                            End If
                        End If
                        Set myPropertyDest = Nothing
                    Next
                End If
    End Sub

    Hope this is making sense....

    Monday, April 2, 2012 10:05 PM
  • I said this before and I'll say it again: if this is an Exchange-only solution, I recommend using Redemption'sRDOFolderSynchronizer object.  I've used it in a few solutions and it saves a lot of effort.  Otherwise, you'll have to do before/after comparisons of all the EntryIDs for every item in the folder to effectively determine what has been removed.  Pick a useful event to trigger this analysis, or use a timer.

    Eric Legault
    MVP (Outlook)
    About me...

    Monday, April 2, 2012 10:45 PM
    Moderator