Working with Distribution Lists

Working with Distribution Lists

You can work with a distribution list (DL) if you have the appropriate permissions in the address list that contains it. Normally you have the required permissions in your personal address book (PAB), in which you can create and modify a private distribution list (PDL). Permissions for the global address list (GAL) are typically limited to a very few profiles, but users with appropriate permissions can modify it as well.

The following code fragment provides two utility functions: AddDLToAL, which creates a new private distribution list in the address list the caller specifies by name; and AddUserToDL, which adds an address entry to a distribution list specified by the caller.

Public Function AddDLToAL(objAL As MAPI.AddressList, _
                          strDLName As String) As MAPI.AddressEntry
' Create a new (private) DL in the specified address list
  Dim colAEs As MAPI.AddressEntries
  Dim objNewAE As MAPI.AddressEntry
  On Error GoTo Err_AddDLToAL

  Set colAEs = objAL.AddressEntries
  Set objNewAE = colAEs.Add("MAPIPDL", strDLName)
  objNewAE.Update ' commit changes to persistent storage
  Set AddDLToAL = objNewAE ' return added PDL (AddressEntry object)
  Exit Function
Err_AddDLToAL:
  Set AddDLToAL = Nothing ' no object was added to address list
  Exit Function
End Function


Public Function AddUserToDL(objDL As MAPI.AddressEntry, _
                            strAEName As String, _
                            strAEAddr As String) As Boolean
' Add an address entry to the specified DL
  Dim objNewAE As MAPI.AddressEntry
  On Error GoTo Err_AddUserToDL

  Set objNewAE = objDL.Members.Add("SMTP", strAEName)
  With objNewAE
    .Address = strAEAddr
    .Fields(CdoPR_COMMENT) = "Added by AddUserToDL function"
    .Fields(CdoPR_COMPANY_NAME) = "Our company"
    .Fields(CdoPR_DEPARTMENT_NAME) = "Your department"
    .Fields(CdoPR_MANAGER_NAME) = "Your manager"
    .Fields(CdoPR_TITLE) = "Dr/Mr/Mrs/Ms/M/Mme/Mlle/Sr/Sra/Herr/Fr"
    .Fields(CdoPR_GIVEN_NAME) = "Your first name"
    .Fields(CdoPR_SURNAME) = "Your last name"
    .Update
  End With
  AddUserToDL = True ' successful
  Exit Function
Err_AddUserToDL
  AddUserToDL = False ' unsuccessful
  Exit Function
End Function

 

See Also

Concepts

Programming Tasks