HOWTO: VB6/CDO 1.21 – expand a distribution list


Below is a sample:


 


' TODO: Create a new VB Project
' TODO: Set a reference to CDO 1.21
' TODO: Add a button to the form and paste this code in.
' TODO: Change TODO: sections below.


Private Sub Command1_Click()


    Dim objSession As MAPI.Session
    Dim ObjMessage As MAPI.Message
    Dim objUseRecips As MAPI.Recipients
    Dim objRecips As MAPI.Recipients
    Dim objRecip As MAPI.Recipient


    Dim strProfileInfo As String
    Dim sServer As String
    Dim sMailbox As String


    '---------------------- Configure -----------
    sServer = "myemailserver"  ' TODO: Change this to the name of you email server
    sMailbox = "mymailbox"    ' TODO: Change this to the name of your mail box


    strProfileInfo = sServer & vbLf & sMailbox


    ' --------------- Startup a MAPI Session -------------------
    Set objSession = CreateObject("MAPI.Session")
    objSession.Logon , , False, True, 0, True, strProfileInfo


    ' --------------- Create a message object -------------------
    Set ObjMessage = objSession.Outbox.Messages.Add
    Set objUseRecips = ObjMessage.Recipients


    ' ---------------   Get a list of recipients ----------------
    ' Add the users or Distribution lists that you would like to
    ' see free/busy information.
    objUseRecips.Add ("mydistgroup")           ' TODO: add email or distribution list
 
    Set objRecips = ExpandDistList(objSession, objUseRecips)


    ' You can now walk through the expanded recipients collection objRecips
    For Each objRecip In objRecips
       Debug.Print objRecip.Name
    Next


End Sub


'----------------------------------------------------------------------------------------------------------------
' This expands the Distribution List (recipients collection).
'  Input:
'     objSession - CDO 1.21 logged-on session
'     objRecips - unexpanded MAPI.Recipeients Collection
'  Returns: Expanded MAPI.Recipeients collection
'----------------------------------------------------------------------------------------------------------------
Private Function ExpandDistList(objSession As MAPI.Session, objRecips As MAPI.Recipients) As Object
    Const cdoDistList = 1


    'Dim objRecips As MAPI.Recipients
    Dim objRecip As MAPI.Recipient
    Dim objMembers As Object
    Dim objMember As Object


    ' ---------------   Get a list of recipients ----------------
    ' Add the users or Distribution lists that you would like to
 
    For Each objRecip In objRecips
        ' If it is a distribution list, do the following.
        If objRecip.AddressEntry.DisplayType = cdoDistList Then


            ' Get the individual members, and add them.
            Set objMembers = objRecip.AddressEntry.Members
            For Each objMember In objMembers
                objRecips.Add (objMember.Address)
            Next


            ' Remove the distribution list from the recipients' collection.
            objRecip.Delete
        End If
    Next


    objRecips.Resolve
    Set ExpandDistList = objRecips
End Function

Comments (2)

  1. randomgurn says:

    Hi Dan,

    This is useful, but I’m having a few problems with it – specifically, I’m not getting all of the members back!

    Via a very convoluted method (create an email to dist list in Outlook, expand the list, save, do a propfind for the to: field with WebDAV), I’ve got the correct list of recipients, of which there are 74.

    Running the method above only returns 36.

    Help! :S  What could I be doing wrong?

    Andy

  2. meza says:

    Hi,

    I am developing an API which is using webdav api to access to  the exchange server and does the jobs as outlook Calendare does.

    I am developing API in C# and now want to write a method that create an meeting.

    With this you need to access to the distribution list (address book) to choose the attendees and send this meetings to them.

    have you any ide how i can access to the address book via Webdav and .NET C#.

    Thanks in advance

    /meza

Skip to main content