How to work with address lists by using the Outlook 10.0 Object Library in Visual Basic .NET (313799)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Outlook 2002

This article was previously published under Q313799

SUMMARY

This article provides examples for working with address lists by using the Microsoft Outlook 10.0 Object Library with Microsoft Visual Basic .NET.

This article describes how to loop through the AddressLists collection to write the name of each available AddressList object to the console window.

This article also describes how to reference AddressEntry objects in an AddressList object by using the GetFirst method and the Name property and the Index property of the AddressEntry object.

Additionally, this article describes how to reference a Distribution List by name and then write the list of members to the console window.

INTRODUCTION

This step-by-step article contains programming examples for common scenarios when working with AddressList objects and AddressEntry objects by using the Outlook 10.0 Object Library in Visual Basic .NET. The "Getting started" section, contains the steps to generate a Visual Basic .NET Console Application project that you can use to work with the code samples in the other sections.

back to the top

Getting started

To use the code samples in this article, you must create an empty Visual Basic .NET Console Application project. You must also add a reference to the Microsoft Outlook 10.0 Object Library to this project. To do this, follow these steps:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Projects types, click Visual Basic Projects.
  4. Under Templates, click Console Application. By default, a module that is named Module1.vb is created.
  5. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. In the Add Reference dialog box, click the COM tab.
    3. On the COM tab, click Microsoft Outlook 10.0 Object Library, click Select, and then click OK.

      If you receive a message to generate wrappers for the library that you selected, click Yes.
back to the top

Enumerate the available AddressList objects

You can loop through the AddressLists collection and then write the name of every available AddressList object to the console window. To do this, follow these steps:
  1. In the code window of the project that you created in the "Getting started" section, replace the existing code with the following code:
    Module Module1
    
        Sub Main()
            ' Create an Outlook application.
            Dim oApp As Outlook._Application = New Outlook.Application()
    
            ' Get the MAPI namespace.
            Dim oNS As Outlook.NameSpace = oApp.Session
    
            ' Get the AddressLists collection.
            Dim oALs As Outlook.AddressLists = oNS.AddressLists
            Console.WriteLine(oALs.Count)
    
            ' Loop through the AddressLists collection.
            Dim i As Integer
            Dim oAL As Outlook.AddressList
            For i = 1 To oALs.Count
                oAL = oALs.Item(i)
                Console.WriteLine(oAL.Name)
            Next
    
            ' Clean up.
            oAL = Nothing
            oALs = Nothing
            oNS = Nothing
            oApp = Nothing
        End Sub
    
    End Module
  2. Add a break point to the last line of code.
  3. Press F5 to build and then run the Console Application project.
  4. Verify that the list of AddressList objects appears in the console window.
back to the top

Reference the AddressEntry objects in the Global Address List

You can reference the AddressEntry objects in an AddressList object by using the GetFirst method together with the Name property and the Index property of the AddressEntry objects. To do this, follow these steps:
  1. In the code window, replace the existing code with the following code:
    Module Module1
    
        Sub Main()
            ' Create an Outlook application.
            Dim oApp As Outlook._Application = New Outlook.Application()
    
            ' Get the MAPI namespace.
            Dim oNS As Outlook.NameSpace = oApp.Session
    
            ' Get the Global Address List.
            Dim oALs As Outlook.AddressLists = oNS.AddressLists
            Dim oGal As Outlook.AddressList = oALs.Item("Global Address List")
            Console.WriteLine(oGal.Name)
    
            ' Get all the entries.
            Dim oEntries As Outlook.AddressEntries = oGal.AddressEntries
    
            ' Get the first user.
            Dim oEntry As Outlook.AddressEntry = oEntries.GetFirst()
    
            ' Output some common properties.
            Console.WriteLine(oEntry.Name)
            Console.WriteLine(oEntry.Address)
            Console.WriteLine(oEntry.Manager)
    
            ' Get the users by name.
            ' TODO: Replace UserName with your recipient name.
            oEntry = oEntries.Item("UserName")
    
            ' Output some common properties.
            Console.WriteLine(oEntry.Name)
            Console.WriteLine(oEntry.Address)
            Console.WriteLine(oEntry.Manager)
    
            ' Get the users by index.
            oEntry = oEntries.Item(2)
    
            ' Output some common properties.
            Console.WriteLine(oEntry.Name)
            Console.WriteLine(oEntry.Address)
            Console.WriteLine(oEntry.Manager)
    
            ' Clean up.
            oApp = Nothing
            oNS = Nothing
            oALs = Nothing
            oGal = Nothing
            oEntries = Nothing
            oEntry = Nothing
        End Sub
    
    End Module
  2. In this sample code, modify the code where you see TODO.
  3. Add a break point to the last line of code.
  4. Press F5 to build and then run the application.
  5. Verify that the retrieved user information appears in the console window.
back to the top

Enumerate the members of an Exchange Distribution List

You can reference a Distribution List by name and then write the list of members to the console window. To do this, follow these steps.

Note This sample code is designed to work with an Exchange Distribution List that is stored in the Global Address List.
  1. In the code window, replace the existing code with the following code:
    Module Module1
    
        Sub Main()
    
            ' TODO: Replace My DL Name with a valid distribution list name.
            Dim sDLName As String = "My DL Name"
    
            ' Create an Outlook application.
            Dim oApp As Outlook._Application = New Outlook.Application()
    
            ' Get the MAPI namespace.
            Dim oNS As Outlook.NameSpace = oApp.Session
    
            ' Get the Global Address List.
            Dim oALs As Outlook.AddressLists = oNS.AddressLists
            Dim oGal As Outlook.AddressList = oALs.Item("Global Address List")
            Console.WriteLine(oGal.Name)
    
            ' Get a specific Distribution List.
            Dim oEntries As Outlook.AddressEntries = oGal.AddressEntries
            ' Reference the Distribution List by name.
            Dim oDL As Outlook.AddressEntry = oEntries.Item(sDLName)
    
            Console.WriteLine(oDL.Name)
            Console.WriteLine(oDL.Address)
    
            ' Get all the members of the Distribution List.
            oEntries = oDL.Members
            Dim oEntry As Outlook.AddressEntry
            Dim i As Integer
    
            For i = 1 To oEntries.Count
                oEntry = oEntries.Item(i)
                Console.WriteLine(oEntry.Name)
            Next
    
            ' Clean up.
            oEntry = Nothing
            oEntries = Nothing
            oGal = Nothing
            oALs = Nothing
            oNS = Nothing
            oApp = Nothing
        End Sub
    
    End Module
  2. In this sample code, modify the code where you see TODO.
  3. Add a break point to the last line of code.
  4. Press F5 to build and then run the application.
  5. Verify that the retrieved user information appears in the console window.
back to the top

Modification Type:MajorLast Reviewed:4/16/2004
Keywords:kbXML kbMsg kbcode kbHOWTOmaster KB313799 kbAudDeveloper