Accessing a Distribution List Programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Outlook 2003. Can anyone provide me with some sample code that would cycle
through the names within a distribution list? I need to check each entry in
a distribution list (named "Chaplains") to determine whether they need to
receive a certain piece of mail. I need to check the Name, Company, and
EMail address of each entry in the distribution list.

I posted this before, but only got one answer that said, "Go find a big book
on Outlook programming and read it." Of course this was no help at all! I
do not need to become an expert programmer, I just need to get a code example
and I will hack it from there.

Please and thank you for any help you can give. God bless.
 
How about something like this (make sure you already have an instantiated
DistListItem that you pass to this procedure):

Sub GetContactFromEachMemberInDistributionList(objDL As Outlook.DistListItem)
On Error Resume Next

Dim objNS As Outlook.NameSpace
Dim intX As Integer, objRec As Outlook.Recipient
Dim myColl As Collection, objContactsFolder As Outlook.MAPIFolder
Dim objItems As Outlook.Items, objContact As Outlook.ContactItem

Set objNS = Application.GetNamespace("MAPI")
Set myColl = New Collection
For intX = 1 To objDL.MemberCount
Set objRec = objDL.GetMember(intX)
myColl.Add objRec.Name
Next

Set objContactsFolder = objNS.GetDefaultFolder(olFolderContacts)
For intX = 1 To myColl.Count
Set objItems = objContactsFolder.Items.Restrict("[FullName] = '" &
myColl.Item(intX) & "'")
If objItems.Count = 1 Then
Set objContact = objItems.Item(1)
Debug.Print objContact.CompanyName
Debug.Print objContact.Email1Address
End If
Next

Set objRec = Nothing
Set myColl = Nothing
Set objContactsFolder = Nothing
Set objContact = Nothing
Set objItems = Nothing
Set objNS = Nothing
End Sub
 
You didn't say whether you're working with an Outlook distribution list or a
distribution list from the Exchange Global Address List. It makes a big
difference.
 
It is an outlook distribution list.

Sue Mosher said:
You didn't say whether you're working with an Outlook distribution list or a
distribution list from the Exchange Global Address List. It makes a big
difference.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Dear Eric:

Hold my hand just one more step please. How do I make an "instantiated
DistListItem" to pass to the procedure? What Dims do I need, etc. Thanks.

Eric Legault said:
How about something like this (make sure you already have an instantiated
DistListItem that you pass to this procedure):

Sub GetContactFromEachMemberInDistributionList(objDL As Outlook.DistListItem)
On Error Resume Next

Dim objNS As Outlook.NameSpace
Dim intX As Integer, objRec As Outlook.Recipient
Dim myColl As Collection, objContactsFolder As Outlook.MAPIFolder
Dim objItems As Outlook.Items, objContact As Outlook.ContactItem

Set objNS = Application.GetNamespace("MAPI")
Set myColl = New Collection
For intX = 1 To objDL.MemberCount
Set objRec = objDL.GetMember(intX)
myColl.Add objRec.Name
Next

Set objContactsFolder = objNS.GetDefaultFolder(olFolderContacts)
For intX = 1 To myColl.Count
Set objItems = objContactsFolder.Items.Restrict("[FullName] = '" &
myColl.Item(intX) & "'")
If objItems.Count = 1 Then
Set objContact = objItems.Item(1)
Debug.Print objContact.CompanyName
Debug.Print objContact.Email1Address
End If
Next

Set objRec = Nothing
Set myColl = Nothing
Set objContactsFolder = Nothing
Set objContact = Nothing
Set objItems = Nothing
Set objNS = Nothing
End Sub

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
--------------------------------------------------
{Private e-mails ignored}
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/

Chaplain Doug said:
Outlook 2003. Can anyone provide me with some sample code that would cycle
through the names within a distribution list? I need to check each entry in
a distribution list (named "Chaplains") to determine whether they need to
receive a certain piece of mail. I need to check the Name, Company, and
EMail address of each entry in the distribution list.

I posted this before, but only got one answer that said, "Go find a big book
on Outlook programming and read it." Of course this was no help at all! I
do not need to become an expert programmer, I just need to get a code example
and I will hack it from there.

Please and thank you for any help you can give. God bless.
 
Then Eric has you on the right track.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Back
Top