Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting a addresslist from a session object in Outlook VBA

Posted on 1999-06-30
3
Medium Priority
?
1,155 Views
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.  
0
Comment
Question by:phiro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
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







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:



   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
0
 
LVL 1

Author Comment

by:phiro
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..
PhiRo
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1521029
Just glad to help..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 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