none
Outlook tasks BeforeItemMove event not triggering RRS feed

  • Question

  • I'm very much a novice at VBA and cobbled together this code, so it's not totally surprising it does not work. I did try...!

    It's to stop a press of the delete button deleting lots of tasks

    Dim WithEvents olkTasks As Outlook.Items

    Private Sub Application_Startup()
      Set olkTasks = Outlook.Session.GetDefaultFolder(olFolderTasks).Items
    End Sub
    Private Sub Application_Quit()
        Set olkTasks = Nothing
    End Sub

    Private Sub olkTasks_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
      Cancel = True
      If (MsgBox("Confirm delete?", vbQuestion + vbYesNo, "Trying to delete a task") = vbYes) Then
        Cancel = False
      End If
    End Sub

    Wednesday, March 18, 2015 5:30 PM

Answers

  • The BeforeItemMove event belongs to the Folder class, not Items.  

    The Items class provides the ItemRemove event which is fired when an item is deleted from the specified collection. For example:

    Public WithEvents myOlItems As Outlook.Items 
     
     
     
    Public Sub Initialize_handler() 
     
     Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items 
     
    End Sub 
     
     
     
    Private Sub myOlItems_ItemRemove() 
     
     Dim myOlMItem As Outlook.MailItem 
     
     If MsgBox("Do you want to notify the Sales Team?", vbYesNo + vbQuestion) = vbYes Then 
     
     Set myOlMItem = Application.CreateItem(olMailItem) 
     
     myOlMItem.To = "Sales Team" 
     
     myOlMItem.Subject = "Remove Contact" 
     
     myOlMItem.Body = "Remove the following contact from your list:" 
     
     myOlMItem.Display 
     
     End If 
     
    End Sub



    Wednesday, March 18, 2015 5:41 PM

All replies

  • The BeforeItemMove event belongs to the Folder class, not Items.  

    The Items class provides the ItemRemove event which is fired when an item is deleted from the specified collection. For example:

    Public WithEvents myOlItems As Outlook.Items 
     
     
     
    Public Sub Initialize_handler() 
     
     Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items 
     
    End Sub 
     
     
     
    Private Sub myOlItems_ItemRemove() 
     
     Dim myOlMItem As Outlook.MailItem 
     
     If MsgBox("Do you want to notify the Sales Team?", vbYesNo + vbQuestion) = vbYes Then 
     
     Set myOlMItem = Application.CreateItem(olMailItem) 
     
     myOlMItem.To = "Sales Team" 
     
     myOlMItem.Subject = "Remove Contact" 
     
     myOlMItem.Body = "Remove the following contact from your list:" 
     
     myOlMItem.Display 
     
     End If 
     
    End Sub



    Wednesday, March 18, 2015 5:41 PM
  • Thanks. The revised code does the trick... :-)

    Dim WithEvents olkTasks As Outlook.Folder

    Private Sub Application_Startup()
      Set olkTasks = Outlook.Session.GetDefaultFolder(olFolderTasks)
    End Sub
    Private Sub Application_Quit()
        Set olkTasks = Nothing
    End Sub

    Private Sub olkTasks_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
      Cancel = True
      If (MsgBox("Confirm delete?", vbQuestion + vbYesNo, "Trying to delete a task") = vbYes) Then
        Cancel = False
      End If
    End Sub

    Wednesday, March 18, 2015 7:24 PM