VBA code to create new Contacts field in Outlook

Discussion in 'Microsoft Excel Programming' started by Guest, Oct 21, 2005.

  1. Guest

    Guest Guest

    I want to check Outlook Contacts to see if a field exists and if not then to
    create it. Please can anyone help me with the code for this?

    I have tried the following to create a field but without success:

    dim objContacts as outlook.application
    Set myProp = objContacts.UserProperties.Add("MyNewField", olText)

    Please help. Thanks
     
    Guest, Oct 21, 2005
    #1
    1. Advertisements

  2. Guest

    michelxld Guest

    Hello Paul

    I hope this help you



    Add a contact

    Sub AddContactOutlook()
    'activate Microsoft Outlook xx.x Object Library
    Dim objOutlook As New Outlook.Application
    Dim objContact As contactItem

    Set objContact = objOutlook.createItem(olContactItem)

    With objContact
    ..email1Address = ""
    ..FirstName = "firstName"
    ..lastName = "lastName"
    ..homeTelephoneNumber = "00 00 00 00 00"
    ..homeAddressCity = "theCity"
    ..Save
    End With
    End Sub



    control if a firstName exist in the contacts list

    Sub controleFirstName_contactsOutlook()
    'activate Microsoft Outlook xx.x Object Library
    Dim olApp As New Outlook.Application
    Dim Cible As Outlook.contactItem
    Dim dossierContacts As Outlook.MAPIFolder

    Set olApp = New Outlook.Application
    Set dossierContacts =
    olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)

    Set Cible = dossierContacts.Items.Find("[FirstName] = ""firstName""")
    If Not Cible Is Nothing Then
    MsgBox "Exist"
    Else
    MsgBox "Does not exist"
    End If
    End Sub



    Regards,
    michel
     
    michelxld, Oct 22, 2005
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    Thank you michelxd but this is not what I was after.

    I have about 250 contacts in Outlook. I also have an Excel address book that
    I have created with about 50 fields, 6 of which do not appear in the
    'standard' Outlook list of contact fields.

    For instance my Excel address book has fields for the details of a partner's
    name ie Title2, FirstName2, LastName2, Birthday2 etc.

    What I want to do is to create extra fields for each contact in the Outlook
    contacts so that I can upload the Excel field values into Outlook.
    For example, if my first contact is 'FileAs' = "Bloggs, Fred" then I wish to
    add a field entitled "Title2" for that contact and enter the value "Mrs", add
    the filed "FirstName2" and enter the value "Freda", add the field "LastName2"
    and enter the value "Bloggs".

    I can add a UserProperty field to the ContactsFolder but cannot get to add
    the field into each contact.

    Any ideas please?
     
    Guest, Oct 22, 2005
    #3
  4. Guest

    michelxld Guest

    Hello Paul

    sorry , i don't undersand your question

    this example controls if a UserProperty named "MyCustomField" exist
    (for all contatcs) , and add it if does not exist


    Sub control_Or_Add_userProperty_contactsOutlook()
    'activate Microsoft Outlook xx.x Object Library
    Dim olApp As New Outlook.Application
    Dim Cible As Outlook.contactItem
    Dim dossierContacts As Outlook.MAPIFolder
    Dim myProp As Outlook.UserProperty

    Set olApp = New Outlook.Application
    Set dossierContacts =
    olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)

    For Each Cible In dossierContacts.Items
    Set myProp = Cible.UserProperties("MyCustomField")

    If myProp Is Nothing Then
    Set myProp = Cible.UserProperties.Add("MyCustomField", olText)
    myProp.Value = "My data"
    Cible.Save
    End If

    Next
    End Sub


    Regards ,
    michel
     
    michelxld, Oct 23, 2005
    #4
  5. Guest

    Guest Guest

    Hello Michel

    Thanks for the post - I can now see the light.

    Many thanks

    Paul
     
    Guest, Oct 24, 2005
    #5
  6. Guest

    Guest Guest

    I tried your code - I get an error "'91' object variable or with block
    variable not set" when it hits the 'set'. Most frustrating. Ideas
     
    Guest, May 5, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.