How Can I List All the Members of a Microsoft Outlook Distribution List?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I list all the members of a Microsoft Outlook distribution list?

— KH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KH. You know, that’s an interesting question. As it turns out, you can – ow! Hey, let go! Ow! Stop it! Come on, give us a chance; we were just about to tell them! Ow!

Um, excuse us. It has come to our attention that, due to a … production error … yesterday’s column inadvertently made a number of less-than-flattering references to the Scripting Editor. (Ow! Come on; that really hurts! We were just about to get to that part.) The Scripting Guys deeply regret any misconceptions that might have arisen due to that column. Needless to say, we have nothing but the utmost respect for the Scripting Editor, who is not only – by far – the smartest and most competent member of the team, but is a truly kind and caring human being as well. As such, we know that the Scripting Editor will accept our humble and sincere apology; we hope that the rest of you will, too.

There; we said it. Now will you let us go? After all, we can’t very well write today’s column if you keep twisting our arms like that.

Ow! You know, that last twist was totally uncalled for.

No, that’s OK; we’ll stop complaining. Your absolutely right: we’d prefer that you didn’t give us something to complain about.

Now, where were we? Oh, right: listing all the members of a Microsoft Outlook distribution list. Here you go, KH; try this baby on for size:

Const olFolderContacts  = 10

Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderContacts)

Set objList = objFolder.Items(“Approved Vendors”)

For i = 1 to objList.MemberCount Set objMember = objList.GetMember(i) Wscript.Echo objMember.Name & “, ” & objMember.Address Next

As you can see, listing all the members of an Outlook distribution list is actually pretty easy. We begin by defining a constant named olFolderContacts and setting the value to 10; that tells the script to look for the distribution list in the Contacts folder (which, by default, is where distribution lists tend to live). We then use these three lines of code to create an instance of the Outlook.Application object and bind us to the MAPI namespace and the Contacts folder:

Set objOutlook = CreateObject(“Outlook.Application”)
Set objNamespace = objOutlook.GetNamespace(“MAPI”)
Set objFolder = objNamespace.GetDefaultFolder(olFolderContacts)

At this point we’re ready for action. Let’s assume that we want to list the members of the Approved Vendors distribution list. To do that, our first step is to use this line of code to connect to the Approved Vendors list:

Set objList = objFolder.Items(“Approved Vendors”)

Nothing special there; we’re simply creating an object reference to the distribution list in question. If we wanted to connect to a different distribution list we’d simply reference the name of that list when we create the object reference:

Set objList = objFolder.Items(“Northwest Region Team”)

Once we make the connection we can then go ahead and list all members. To do that we set up a For Next loop that runs from 1 to the number of people who are members of the list. How are we supposed to know how many people are on the list? Beats us. But, then again, we don’t have to know; instead, we can let Outlook figure that out for us by using the MemberCount property:

For i = 1 to objList.MemberCount

Inside this loop we use the GetMember method to retrieve the individual members of the list, one at a time, and referencing each member by index number (the counter variable i):

Set objMember = objList.GetMember(i) 

Each time we call GetMember we’ll get back an AddressEntry object. In turn, that enables us to echo back the values of the Name and Address properties:

Wscript.Echo objMember.Name & “, ” & objMember.Address 

And that’s going to give us output similar to this:

Ken Myer,
Pilar Ackerman,
Jonathan Haas

Ow! Stop it; we were just going to tell them that! Give us a chance, for crying out loud.

No, ma’am; we wee definitely not complaining about you. No way.

As the Scripting Editor … politely … reminded us, any time you try and retrieve information about a distribution list’s membership a dialog box will pop us asking you if you want to allow access to that information. If you say “Yes” then the script will retrieve and display the requested data. If you say “No,” or if you ignore the dialog box altogether, then nothing will be retrieved and nothing will be displayed. The moral of that story? You can’t run this script in unattended fashion; instead, you’ll need to start the script and respond to the dialog box. At that point you can let the script run by itself, although at that point it will probably take only a few seconds for the script to complete its work. (Depending, of course, on the number of people who are part of the distribution list.)

Because of that you might prefer to list the all the members of all your distribution lists in one fell swoop. Hey, no problem. Assuming that all your distribution lists reside in the Contacts folder this should do the trick:

Const olFolderContacts  = 10

Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderContacts) Set objItems = objFolder.Items

For i = 1 to objItems.Count If TypeName(objItems.Item(i)) = “DistListItem” Then Set objList = objItems.Item(i) Wscript.Echo objList.DLName For j = 1 to objList.MemberCount Set objMember = objList.GetMember(j) Wscript.Echo objMember.Name & “, ” & objMember.Address Next Wscript.Echo End If Next

In conclusion, we’d like to reiterate that everything we said in today’s column we said of our own free will, and no one – least of all our beloved Scripting Editor – threatened us in any way, shape, or form. Thank you.

Note to the Scripting Editor. OK, we said it; now can Dean have his kids back? Oh, come on: you said if we included the above disclaimer he could get both of them back. Well, you’re the boss. But he’s not going to be very happy about this.


Discussion is closed.

Feedback usabilla icon