How to: Display the Address Lists for a Profile

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

Programming Applications for Office Outlook 2007

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007, from Microsoft Press (ISBN 9780735622494, copyright Microsoft Press 2007, all rights reserved).

Buy this book

Sample chapters

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 Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.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 =
    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());

Other Resources