vbaoutlookcontactsuser-defined-fields

Create User Defined Contact Field in Outlook using VBA


I am trying to write an outlook Macro that will create a new field on all Contacts and then populate that field based on another already existing field. Here's what I have so far. It doesn't seem to create anything.

Sub ChangeField()
    Dim ContactsFolder As Folder
    Dim Contact As ContactItem
    Dim objProperty As UserDefinedProperty

    Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
    MsgBox ("Contacts Found: " & ContactsFolder.Items.Count)

    With ContactsFolder.UserDefinedProperties
        Set objProperty = .Add("CustomCompanyField", olText)
    End With

    For Each Contact In ContactsFolder.Items
        Contact.CustomCompanyField = Contact.CompanyName
        Contact.Save
    Next    
End Sub

Can this be done through VBA? Or can contact fields only be created manually? Any help is appreciated!


Solution

  • Try to replace the line

     Contact.CustomCompanyField = Contact.CompanyName
    

    with

     Contact.UserProperties.Add("CustomCompanyField", olText).Value = Contact.CompanyName