SeanS
SeanS

Reputation: 62

How to print the name and value of each Outlook Contact field with VBA

For each Outlook Contact I need to grab the name and value of each field.
Eg.

    FirstName:  John
    LastNmae:  Doe
... etc.

How can I go about this without referencing each field individually?

With the code below I can print the name of each property, but I don't know how to print the value. The line that is commented out throws an error: "Invalid procedure call or argument"

Dim ContactsFolder As Folder
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
Dim Contact As ContactItem
Dim i As Integer

    For Each Contact In ContactsFolder.Items
        id = Contact.EntryID
        Debug.Print Contact.FirstName
        Debug.Print id
        For i = 0 To Contact.ItemProperties.Count - 1
            Debug.Print Contact.ItemProperties(i).Name
            'Debug.Print Contact.ItemProperties(i).Value
        Next
    Next

Upvotes: 1

Views: 1707

Answers (1)

Cody Geisler
Cody Geisler

Reputation: 8617

Here is an example of grabbing all the names of all address lists in the current session.

You can find all the properties for the object at https://msdn.microsoft.com/en-us/library/office/dn320232.aspx

For obtaining the object's properties' names and evaluating them, see a TypeLib reference. (http://visualbasic.happycodings.com/applications-vba/code19.html may or may not apply)

Sub getContact()

    Dim ContactsFolder As Folder
    Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
    Dim Contact As ContactItem
    Dim i As Integer

    For Each Contact In ContactsFolder.Items
        ID = Contact.EntryID
        Debug.Print Contact.FirstName
        Debug.Print ID
        For i = 0 To Contact.ItemProperties.Count - 1
        Debug.Print Contact.ItemProperties(i).Name
        ' Doesn't work for object propertieties, like application.
        ' (Do Error Handling)
        Deubg.Print CallByName(Contact, Contact.ItemProperties(i).Name, VbGet) 
    Next
Next


End Sub

Upvotes: 1

Related Questions