Address List in Outlook Add-in using VBA code example

Address List in Outlook Add-in using VBA code example

AddressEntries

Collection contains one or more AddressEntry objects and each AddressEntry object contains various objects, properties for address book in Outlook. In this article we will write code to iterate List of address from a address book.

Code example

Public Sub AddressEntriesExample()

    'Outlook application object declaration
    Dim oApp As Outlook.Application
    
    'bind current outlook instance
    Set oApp = GetObject("", "Outlook.Application")
    
    'Bind namespace
    Dim oNameSpace As Outlook.Namespace
    Set oNameSpace = oApp.GetNamespace("MAPI")
    
    'Bind all address lists available in outlook
    Dim oAddressLists As AddressLists
    Set oAddressLists = oNameSpace.AddressLists
    
    'Iterate each list
    Dim oAddList As AddressList
    For Each oAddList In oAddressLists
        
        'Bind Address Entries under each list
        Dim oAddressEntries As AddressEntries
        Set oAddressEntries = oAddList.AddressEntries
        
        'Iterate each address entry
        Dim oAddEntry As AddressEntry
        For Each oAddEntry In oAddressEntries
            Debug.Print oAddEntry.Address
            Debug.Print oAddEntry.Name
            Debug.Print oAddEntry.Type
        Next oAddEntry
    
    Next oAddList
    
    'Cleanup
    If Not oNameSpace Is Nothing Then
        Set oNameSpace = Nothing
    End If
    If Not oAddressLists Is Nothing Then
        Set oAddressLists = Nothing
    End If
    If Not oAddList Is Nothing Then
        Set oAddList = Nothing
    End If
    If Not oAddEntry Is Nothing Then
        Set oAddEntry = Nothing
    End If
    If Not oAddressEntries Is Nothing Then
        Set oAddressEntries = Nothing
    End If
    If Not oApp Is Nothing Then
        Set oApp = nohting
    End If
End Sub

Code explanation

Each address list object exposes AddressEntries collection and each AddressEntries collection provide access to each AddressEntry object which exposes various properties and methods.

Next >> Action object in Outlook Plugin with VBA Code example

Leave a Reply

Your email address will not be published. Required fields are marked *