HOWTO: Access MAPI Address Books with Active Messaging 1.1

ID: Q172093


The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0
  • Microsoft Access versions 7.0, 97
  • Microsoft Word for Windows
  • Microsoft Excel for Windows
  • Microsoft Excel for Windows 95, version 7.0


SUMMARY

Active Messaging 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 position. The sample below walks the AddressLists collection displaying information about each accessible Address Book. The second part of the sample selects the Global Address List and then walks the Address Entries, recursively expanding Distribution Lists.


MORE INFORMATION

The following sample code applies to any 32-bit VBA-based product.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Step-by-Step Example

  1. Create a new VBA project. Add a Reference to Microsoft Active Messaging 1.1 Object Library.


  2. 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 readonly access flag
                   Debug.Print objAddressList.IsReadOnly,
    
                   ' display count of recipients
                   Set collAddressEntries = objAddressList.AddressEntries
                   Debug.Print Str(collAddressEntries.Count)
                   ' if any AddressEntries, display 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 session and logoff
              objSession.Logoff
              Exit Sub
    
          error_olemsg:
                   MsgBox "Error " & Str(Err) & ": " & Error$(Err)
                   Exit Sub
    
          End Sub
    
          ' Walk an AddressEntries collection recursively expanding
          ' 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 


  3. Visual Basic only: Set Sub Main to run in the Project Properties, and then run.

    Other VBA applications: In the Debug/Immediate Window, type "MAIN" and press the ENTER key.


  4. The output will appear in the Debug/Immediate window.


Notes

The address book can have restricted entries. These will generate an "Access Denied" message:
Error -2147024891: [Active Messaging - [E_ACCESSDENIED(80070005)]]
It is left up to the user to add extra error handling where required.


REFERENCES

Documentation for Active Messaging is available on MSDN under the SDK Documentation/Platform SDK/Databases and Messaging Services/Active Messaging topics.

The following Microsoft Knowledge Base article lists various sources for installing Active Messaging 1.1:

Q171440 INFO: Where to Acquire the Collaboration Data Objects Libraries
For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:
Q176916 INFO: Active Messaging and Collaboration Data Objects (CDO)

Additional query words:

Keywords : kbprg kbAccess97 kbActMsg kbExcel kbMAPI KbVBA kbVBp400 kbVBp500
Version : WINDOWS:4.0,5.0,7.0,97; Win95:7.0
Platform : Win95 WINDOWS
Issue type : kbhowto


Last Reviewed: November 3, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.