Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 172093 - Last Review: October 25, 2007 - Revision: 6.4
How to access MAPI Address Books by using Collaboration Data Objects 1.x
This article was previously published under Q172093
On This Page
SUMMARY
Collaboration Data Objects (CDO) 1.1 supports programmatic access to MAPI Address Books
through the
AddressEntries collection of an
AddressList object. Each
AddressList object corresponds to a MAPI Address Book and is a member of
the
AddressLists collection of the
Session object.
The available Address
Books can be enumerated by walking the
AddressLists collection, and an
individual address book can be selected by indexing the
AddressLists
collection by name or by position.
The following sample code walks the
AddressLists
collection, and displays information about each address book that can be accessed. The
second part of the sample selects the global address list (GAL) and then walks
the Address Entries, recursively expanding Distribution Lists.
MORE INFORMATION
The following sample code applies to any 32-bit Visual Basic-based product.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Step-by-step example
- Create a new Visual Basic project. Add a reference to the Collaboration Data Objects
1.1 Object Library.
- Type the following code into a module.
Option Explicit
Sub main()
Dim objSession As MAPI.Session ' Use early binding for
' efficiency.
Dim collAddressLists As AddressLists
Dim objAddressList As AddressList
Dim collAddressEntries As AddressEntries
On Error GoTo error_olemsg
' Create a session and log on.
Set objSession = CreateObject("MAPI.Session")
' Use a valid Exchange profile name.
objSession.Logon ("<Your Profile Name Here>")
'Walk the available address books.
Set collAddressLists = objSession.AddressLists
For Each objAddressList In collAddressLists
' Display collection selection indices.
Debug.Print objAddressList.Name,
Debug.Print objAddressList.Index,
' Display the readonly access flag.
Debug.Print objAddressList.IsReadOnly,
' Display the count of recipients.
Set collAddressEntries = objAddressList.AddressEntries
Debug.Print Str(collAddressEntries.Count)
' If there are any AddressEntries, display the first recipient name.
If Not objAddressList.AddressEntries(1) Is Nothing Then _
Debug.Print objAddressList.AddressEntries(1).Name
Next objAddressList
' Walk the global address list.
Set collAddressEntries = objSession.AddressLists( _
"Global Address List").AddressEntries
WalkAddressList collAddressEntries
' Close the session and log off.
objSession.Logoff
Exit Sub
error_olemsg:
MsgBox "Error " & Str(Err) & ": " & Error$(Err)
Exit Sub
End Sub
' Walk an AddressEntries collection and recursively expand
' Distribution Lists.
Sub WalkAddressList(collAddressEntries As AddressEntries)
Dim objaddressEntry As AddressEntry
For Each objaddressEntry In collAddressEntries
' Display the recipient's name.
Debug.Print objaddressEntry.Name,
' Display the recipient's type.
Select Case objaddressEntry.DisplayType
Case ActMsgUser
Debug.Print "Exchange Recipient"
Case ActMsgDistList
Debug.Print "Distribution List"
WalkAddressList objaddressEntry.Members
Debug.Print "End of DL " & objaddressEntry.Name
Case ActMsgForum
Debug.Print "Public Folder"
Case ActMsgAgent
Debug.Print "Agent"
Case ActMsgPrivateDistList
Debug.Print "Private DL"
Case ActMsgOrganization
Debug.Print "Organization"
Case ActMsgRemoteUser
Debug.Print "Custom Recipient"
Case Else
Debug.Print "Unknown type"
End Select
Next objaddressEntry
End Sub - Visual Basic only: Set Sub Main to run in the Project Properties, and
then run the application.
Other VBA applications: In the Debug/Immediate window, type MAIN,
and then press ENTER.
The output will appear in the Debug/Immediate window.
Notes
The address book can have restricted entries. Restricted entries will generate an
"Access Denied" message:
Error -2147024891: [Collaboration Data Objects - [E_ACCESSDENIED(80070005)]]
The user can include additional error handling where required.
REFERENCES
Documentation for Active Messaging is available in the Microsoft Developer Network (MSDN) Library under the following topics:
- SDK
Documentation
- Platform SDK
- Databases and Messaging Services
- Active
Messaging
For more information about how to obtain Collaboration Data Objects Libraries, click the following article number to view the article in the Microsoft Knowledge Base:
171440Â
(http://kbalertz.com/Feedback.aspx?kbNumber=171440/
)
Where to acquire the CDO Libraries (all versions)
APPLIES TO
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Outlook 98 Standard Edition
- Microsoft Outlook 2000 Standard Edition
- Microsoft Outlook 2002 Standard Edition
- Microsoft Office Outlook 2003
- Microsoft Exchange Server 5.5 Standard Edition
- Microsoft Exchange Server 2003 Standard Edition
- Microsoft Exchange Server 2003 Enterprise Edition
- Microsoft Collaboration Data Objects 2.0
- Microsoft Collaboration Data Objects 1.1
- Microsoft Collaboration Data Objects 1.21
| kbhowto kbprogramming KB172093 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please