Display the address lists for a profile

This example shows how to display the address lists for the current profile.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

The current profile contains address lists that are represented by the AddressLists collection. To get an instance of the AddressLists collection, you must use the AddressLists property of the NameSpace object.

In the following code example, EnumerateAddressLists first enumerates each AddressList object in the AddressLists collection by using a foreach statement. The example then creates a string that contains the values of the Name, ResolutionOrder, IsReadOnly, and IsInitialAddressList properties. Finally, EnumerateAddressLists writes the string to the trace listeners of the Listeners collection. This displays each address list for the current profile.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void EnumerateAddressLists()
{
    Outlook.AddressLists addrLists =
         Application.Session.AddressLists;
    foreach (Outlook.AddressList addrList in addrLists)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("Display Name: " + addrList.Name);
        sb.AppendLine("Resolution Order: "
            + addrList.ResolutionOrder.ToString());
        sb.AppendLine("Read-only : "
            + addrList.IsReadOnly.ToString());
        sb.AppendLine("Initial Address List: "
            + addrList.IsInitialAddressList.ToString());
        sb.AppendLine("");
        Debug.WriteLine(sb.ToString());
    }
}

See also