none
Catching contact delete event VB RRS feed

  • Question

  • I have a form appended to the contact form in Outlook 2007 and would like to execute some code if the contact is deleted. I haven't been able to find out how to set a listener for this event however, does anyone know a good tutorial page for doing this?
    Thursday, October 10, 2013 6:33 PM

Answers

  • You can use ContactItem.BeforeDelete event.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, October 11, 2013 2:46 AM
  • The correct signature for BeforeDelete() is this:

    Private Sub myItem_BeforeDelete(Item AsObject, ByRefCancel AsBoolean) HandlesoItem.BeforeDelete

    If you select your myItem object in the left-hand dropdown then all the events available for that type of object are listed in the right-hand dropdown at the top of the code window. Selecting one of the events will insert a template for the event handler in your code.

    You can find COM addin examples at the Office Developer Web site at MSDN, and you can find examples and lots of code samples at www.outlookcode.com


    Ken Slovak MVP - Outlook


    • Edited by Ken Slovak MVP Friday, October 25, 2013 2:26 PM
    • Marked as answer by bmcn Friday, October 25, 2013 2:49 PM
    Friday, October 25, 2013 2:26 PM

All replies

  • You can use ContactItem.BeforeDelete event.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, October 11, 2013 2:46 AM
  • I was looking for a way to do that, I've not been able to find any tutorial sites for outlook addins.

    The msdn site has this example for catching a mailitem.beforedelete event: http://msdn.microsoft.com/en-us/library/office/ff861266.aspx so I tried to mirror that usage for my contactitem but it did not work, even though this page suggest that it should as far as I can tell: http://msdn.microsoft.com/en-us/library/office/ff868306.aspx.

    Under this contactItem page (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.contactitem_events.aspx) I can click on the beforeDelete event to land here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.itemevents_10_event.beforedelete.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 but it does not show how to actually implement my code?

    Googling a way to do this has led me nowhere. Here is my current code:

    Public Class FormRegion1
    Public WithEvents myItem As Outlook.ContactItem
    
    
    
    'Occurs before the form region is displayed. 
    'Use Me.OutlookItem to get a reference to the current Outlook item.
    'Use Me.OutlookFormRegion to get a reference to the form region.
    Private Sub FormRegion1_FormRegionShowing(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.FormRegionShowing
            myItem = CType(Me.OutlookItem, Outlook.ContactItem)
    
    End Sub
    
    Private Sub myItem_BeforeDelete(ByVal Item As Object, Cancel As Boolean)
        'Prompts the user before deleting an item
        Dim strPrompt As String
        'Prompt the user for a response 
        strPrompt = "This text does not appear"
        If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbNo Then
            'Don't delete the item 
            Cancel = True
        End If
    End Sub

    Friday, October 25, 2013 1:31 PM
  • The correct signature for BeforeDelete() is this:

    Private Sub myItem_BeforeDelete(Item AsObject, ByRefCancel AsBoolean) HandlesoItem.BeforeDelete

    If you select your myItem object in the left-hand dropdown then all the events available for that type of object are listed in the right-hand dropdown at the top of the code window. Selecting one of the events will insert a template for the event handler in your code.

    You can find COM addin examples at the Office Developer Web site at MSDN, and you can find examples and lots of code samples at www.outlookcode.com


    Ken Slovak MVP - Outlook


    • Edited by Ken Slovak MVP Friday, October 25, 2013 2:26 PM
    • Marked as answer by bmcn Friday, October 25, 2013 2:49 PM
    Friday, October 25, 2013 2:26 PM
  • Thank you for that, I thought that was available for elements placed in design mode only. It's up and working just fine now!
    Friday, October 25, 2013 2:54 PM