{"id":65223,"date":"2007-03-27T03:03:00","date_gmt":"2007-03-27T03:03:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/27\/how-can-i-list-all-the-members-of-a-microsoft-outlook-distribution-list\/"},"modified":"2007-03-27T03:03:00","modified_gmt":"2007-03-27T03:03:00","slug":"how-can-i-list-all-the-members-of-a-microsoft-outlook-distribution-list","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-members-of-a-microsoft-outlook-distribution-list\/","title":{"rendered":"How Can I List All the Members of a Microsoft Outlook Distribution List?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! How can I list all the members of a Microsoft Outlook distribution list?<BR><BR>&#8212; KH<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, KH. You know, that\u2019s an interesting question. As it turns out, you can \u2013 ow! Hey, let go! Ow! Stop it! Come on, give us a chance; we were just about to tell them! Ow!<\/P>\n<P>Um, excuse us. It has come to our attention that, due to a \u2026 production error \u2026 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar07\/hey0326.mspx\"><B>yesterday\u2019s column<\/B><\/A> 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 \u2013 by far \u2013 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.<\/P>\n<P>There; we said it. <I>Now<\/I> will you let us go? After all, we can\u2019t very well write today\u2019s column if you keep twisting our arms like that.<\/P>\n<P>Ow! You know, that last twist was totally uncalled for.<\/P>\n<P>No, that\u2019s OK; we\u2019ll stop complaining. Your absolutely right: we\u2019d prefer that you <I>didn\u2019t<\/I> give us something to complain about.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">Const olFolderContacts  = 10<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderContacts)<\/p>\n<p>Set objList = objFolder.Items(&#8220;Approved Vendors&#8221;)<\/p>\n<p>For i = 1 to objList.MemberCount\n    Set objMember = objList.GetMember(i)\n    Wscript.Echo objMember.Name &amp; &#8220;, &#8221; &amp; objMember.Address \nNext\n<\/PRE>\n<P>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 <B>Outlook.Application<\/B> object and bind us to the <B>MAPI<\/B> namespace and the Contacts folder:<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderContacts)\n<\/PRE>\n<P>At this point we\u2019re ready for action. Let\u2019s assume that we want to list the members of the <I>Approved Vendors<\/I> distribution list. To do that, our first step is to use this line of code to connect to the Approved Vendors list:<\/P><PRE class=\"codeSample\">Set objList = objFolder.Items(&#8220;Approved Vendors&#8221;)\n<\/PRE>\n<P>Nothing special there; we\u2019re simply creating an object reference to the distribution list in question. If we wanted to connect to a different distribution list we\u2019d simply reference the name of <I>that<\/I> list when we create the object reference:<\/P><PRE class=\"codeSample\">Set objList = objFolder.Items(&#8220;Northwest Region Team&#8221;)\n<\/PRE>\n<P>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 <I>know<\/I> how many people are on the list? Beats us. But, then again, we don\u2019t <I>have<\/I> to know; instead, we can let Outlook figure that out for us by using the <B>MemberCount<\/B> property:<\/P><PRE class=\"codeSample\">For i = 1 to objList.MemberCount\n<\/PRE>\n<P>Inside this loop we use the <B>GetMember<\/B> method to retrieve the individual members of the list, one at a time, and referencing each member by index number (the counter variable i):<\/P><PRE class=\"codeSample\">Set objMember = objList.GetMember(i) \n<\/PRE>\n<P>Each time we call GetMember we\u2019ll get back an <B>AddressEntry<\/B> object. In turn, that enables us to echo back the values of the <B>Name<\/B> and <B>Address<\/B> properties:<\/P><PRE class=\"codeSample\">Wscript.Echo objMember.Name &amp; &#8220;, &#8221; &amp; objMember.Address \n<\/PRE>\n<P>And that\u2019s going to give us output similar to this:<\/P><PRE class=\"codeSample\">Ken Myer, kenmyer@fabrikam.com\nPilar Ackerman, pilarackerman@contoso.com\nJonathan Haas jhaas@wingtiptoys.com\n<\/PRE>\n<P>Ow! Stop it; we were just going to tell them that! Give us a chance, for crying out loud.<\/P>\n<P>No, ma\u2019am; we wee definitely <I>not<\/I> complaining about you. No way.<\/P>\n<P>As the Scripting Editor \u2026 politely \u2026 reminded us, any time you try and retrieve information about a distribution list\u2019s membership a dialog box will pop us asking you if you want to allow access to that information. If you say \u201cYes\u201d then the script will retrieve and display the requested data. If you say \u201cNo,\u201d or if you ignore the dialog box altogether, then nothing will be retrieved and nothing will be displayed. The moral of <I>that<\/I> story? You can\u2019t run this script in unattended fashion; instead, you\u2019ll need to start the script and respond to the dialog box. At that point you <I>can<\/I> 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.)<\/P>\n<P>Because of that you might prefer to list the all the members of <I>all<\/I> 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:<\/P><PRE class=\"codeSample\">Const olFolderContacts  = 10<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderContacts)\nSet objItems = objFolder.Items<\/p>\n<p>For i = 1 to objItems.Count\n    If TypeName(objItems.Item(i)) = &#8220;DistListItem&#8221; Then\n        Set objList = objItems.Item(i) \n        Wscript.Echo objList.DLName\n        For j = 1 to objList.MemberCount\n            Set objMember = objList.GetMember(j)\n            Wscript.Echo objMember.Name &amp; &#8220;, &#8221; &amp; objMember.Address\n        Next\n        Wscript.Echo\n    End If\nNext\n<\/PRE>\n<P>In conclusion, we\u2019d like to reiterate that everything we said in today\u2019s column we said of our own free will, and no one \u2013 least of all our beloved Scripting Editor \u2013 threatened us in any way, shape, or form. Thank you.<\/P>\n<TABLE class=\"dataTable\" id=\"E1F\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note to the Scripting Editor<\/B>. OK, we said it; <I>now<\/I> can Dean have his kids back? Oh, come on: you said if we included the above disclaimer he could get <I>both<\/I> of them back. Well, you\u2019re the boss. But he\u2019s not going to be very happy about this.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the members of a Microsoft Outlook distribution list?&#8212; KH Hey, KH. You know, that\u2019s an interesting question. As it turns out, you can \u2013 ow! Hey, let go! Ow! Stop it! Come on, give us a chance; we were just about to tell them! Ow! Um, excuse [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[212,49,3,5],"class_list":["post-65223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I list all the members of a Microsoft Outlook distribution list?&#8212; KH Hey, KH. You know, that\u2019s an interesting question. As it turns out, you can \u2013 ow! Hey, let go! Ow! Stop it! Come on, give us a chance; we were just about to tell them! Ow! Um, excuse [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}