Microsoft KB Archive/313799

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

Article ID: 313799

Article Last Modified on 4/16/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Outlook 2002 Standard Edition

-



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.''



IN THIS TASK

 * INTRODUCTION
 * Getting started
 * Enumerate the available AddressList objects
 * Reference the AddressEntry objects in the Global Address List
 * Enumerate the members of an Exchange Distribution List



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 &quot;Getting started&quot; 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:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Projects types, click Visual Basic Projects. Under Templates, click Console Application. By default, a module that is named Module1.vb is created. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:  On the Project menu, click Add Reference.</li> In the Add Reference dialog box, click the COM tab.</li> 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.</li></ol> </li></ol>

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: <ol>  In the code window of the project that you created in the &quot;Getting started&quot; 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 </li> Add a break point to the last line of code.</li> Press F5 to build and then run the Console Application project.</li> Verify that the list of AddressList objects appears in the console window.</li></ol>

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: <ol>  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(&quot;Global Address List&quot;) 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(&quot;UserName&quot;)

' 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 </li> In this sample code, modify the code where you see TODO.</li> Add a break point to the last line of code.</li> Press F5 to build and then run the application.</li> Verify that the retrieved user information appears in the console window.</li></ol>

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. <ol>  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 = &quot;My DL Name&quot;

' 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(&quot;Global Address List&quot;) 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 </li> In this sample code, modify the code where you see TODO.</li> Add a break point to the last line of code.</li> Press F5 to build and then run the application.</li> Verify that the retrieved user information appears in the console window.</li></ol>

back to the top

Additional query words: outsol outsol2002 OOM model

Keywords: kbhowtomaster kbmsg kbxml kbcode KB313799

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.