Outlook 2007 - Macro to Delete Email Address RRS feed

  • Question

  • The following macro deletes the email address of the emal2 address in the contact person, but it does not show up as deleted unless I save and close the contact and then open up the contact, which then shows that the email2 address is not there.  Is there a way that it shows it deletes it and I don't have to save and close the contact and open it up?  thanks much

    Public Sub DeleteEmail2()
        Dim currentExplorer As Explorer
        Dim Selection As Selection
        Dim obj As Object
        Set currentExplorer = Application.ActiveExplorer
        Set Selection = currentExplorer.Selection
        On Error Resume Next
        For Each obj In Selection
                Set objContact = obj
         With objContact
               If .Email2Address <> "" Then
               .Email2Address = ""
              End If
         End With
        Set obj = Nothing
        Set objContact = Nothing
        End Sub

    Sunday, August 9, 2015 2:52 PM

All replies

  • Hello Imseaz,

    Outlook may not reflect changes in the UI until you re-open the item and release all references, it caches the values. This is a known issue when dealing with OOM.

    Sunday, August 9, 2015 2:59 PM
  • Here is the macro that deletes the email address of the first email address, and I do not have to save and close and open the contact, as it deletes it immediately when the contact is open.  Can I change this macro so it does it for the Email2 address?

    Sub DeleteEmail1()
      Dim objApp As Application
      Dim objNS As NameSpace
      Dim objFolder As MAPIFolder
      Dim objItem As Object

      Set objApp = CreateObject("Outlook.Application")
      Set objNS = objApp.GetNamespace("MAPI")
      On Error Resume Next

    If TypeName(objApp.ActiveWindow) = "Inspector" Then
    Set objItem = objApp.ActiveInspector.currentItem

      objItem.UserProperties("E-mail") = ""
    GoTo Leave
    End If

         Set objSelection = objApp.ActiveExplorer.Selection
               For Each objItem In objSelection
          objItem.UserProperties("E-mail") = ""
      Set objItem = Nothing
      Set objFolder = Nothing
      Set objNS = Nothing
      Set objApp = Nothing
    End Sub

    Sunday, August 9, 2015 3:08 PM
  • I have noticed the following line of code:

    Set objApp = CreateObject("Outlook.Application")

    There is no need to create a new Outlook Application instance from Outlook VBA macro. You should use the Application property available out of the box.

    See Getting Started with VBA in Outlook 2010 for more information.

    Sunday, August 9, 2015 3:11 PM
  • Thanks for replying right away, but I don't understand what you are saying.   Can I just change the second macro I posted to the emal2address?   Or fix the first macro I showed so it just does the deleted without saving and closing and opening up the contact again?
    Sunday, August 9, 2015 3:17 PM
  • Imseaz,

    Please understand that the forum is for developers and you need to understand at least the basics of VBA programming. How can others help if you don't want learn anything?

    > Can I just change the second macro I posted to the emal2address?  

    It is up to you which way is to choose. You can try to use any low-level property viewer such as MFCMAPI or OutlookSpy for observing all user properties.

    Sunday, August 9, 2015 3:27 PM
  • What other forum should I got to so they can show me what to do?
    Sunday, August 9, 2015 3:39 PM
  • There is no forums to get a ready made code. You need to work on the code and if you have any specific Outlook dev questions you are free to ask there.
    Monday, August 10, 2015 12:43 PM
  • Hi,

    I have created a new contact item, and run the code DeleteEmail2() to reset the property Email2Address , the code works fine, even if I didn't save the changes. There is no need to save it and reopen it. 

    Did I miss something ?

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 11, 2015 9:38 AM
  • Can you please post your code that I can try to use?
    Tuesday, August 11, 2015 6:03 PM