HOWTO: CDO 1.21/VB Expand a Distribution List


'HOWTO: CDO 1.21/VB Expand a Distribution List


' 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 (0)

Skip to main content