Getting a addresslist from a session object in Outlook VBA

Posted on 1999-06-30
Medium Priority
Last Modified: 2008-02-20
I have a function in Access that creates a CDO (Used to be called Active Messaging) session.
From this session I try to get the Address list. But I get a type mismatch on anything I do
This is a example of the code I use.
  Dim Ses As Session
    Dim outL As Outlook.Application
    Dim adrList As AddressList
    Set outL = GetObject(, "Outlook.Application")
    Set Ses = outL.Session
    Set adrList = Ses.GetAddressList(CdoAddressListGAL)

It is not only this one, non of the objects linked to the Session object can be set to another object. Everything fails with a Type mismatch.
Every permutation that came to my mind I have tried. declaring all the var's as Objects etc. References are to Outlook98 Objects and CDO 1.21. Tried it on new databases.
OS : win95, Exchange 5.5, Access97, Outlook98. I work at the client side of the mail application.
The Session object is a valid object of a running session with Outlook.

One extra oddity is that setting a reference to Access of the CDO object - library adds it's objects twice. So if I do a Dim x as ... generates a list with the CDO objects twice.
There is only one instance of the cdo.dll present on this system or in the paths specified by the PATH variable.  
Question by:phiro
  • 2
LVL 13

Accepted Solution

Mirkwood earned 300 total points
ID: 1521027
HOWTO: Access MAPI Address Books with Active Messaging 1.1

Last reviewed: January 8, 1998
Article 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 97 for Windows Microsoft Excel 97 for Windows Microsoft Excel for Windows 95, version 7.0


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.


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
          Exit Sub

               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.


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.


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:

   ARTICLE-ID: Q171440
   TITLE     : INFO: Where to Acquire the Active Messaging Libraries

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

Keywords          : kbprg ActMsg vb432 VB4WIN vb5all vb5howto vbwin
Technology        : kbvba
Version           : WINDOWS:4.0 5.0 7.0 97
Platform          : WINDOWS
Issue type        : kbhowto

Author Comment

ID: 1521028
Hi Mirkwood.
I had tried the micrsoft solution already and found out it gave the same errors. But the moment I read your reply the coin fell. I complained of the double references in the object selector? It came to me that if there are two Session objects, VB didn't know which one to choose. So an type mismatch occurred. If I use the full reference to the object: MAPI.Session in this case. it works fine. The CDO lib has two ways of connecting to the exchange server. CDO and ADSI. The both have the same objects... So everything appeared double.... I feel quite dumb...
Thanx for resetting me on the right path..
LVL 13

Expert Comment

ID: 1521029
Just glad to help..

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question