Also works with Outlook's Contact Groups (which are distribution lists with a friendlier name).
Run the macro, enter a contact's name in the Input box then press OK to create a list of the DLs the contact belongs to in a Word document. Use the exact name to return only that contact, or a partial name to return all contacts with similar names.
Leave the field blank and press OK to create a list of the members of all DLs.
A sample of the output when getting a list of all DLs and members (the name field is left blank). Click the image to see the full version with multiple DLs listed.:
This works in all versions of Outlook.
To search by email address, use GetMember(y).Address instead of GetMember(y).Name. (It's used 4 times in the code.) This will find all partial matches, for example, searching for 'poremsky' will find all addresses containing my last name anywhere in the email address.)
VBA Code sample
A text file of this macro is here.
Copy and paste this code into Outlook's VBA editor and set the references to Word.
' Code sample written by Graham Mayor - Word MVP ' //www.gmayor.com Sub ListNames() Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim myNameSpace As Outlook.NameSpace Dim myFolder As Outlook.Folder 'use Outlook.MAPIFolder if Outlook.folder fails. Dim myDistList As Outlook.DistListItem Dim myFolderItems As Outlook.Items Dim myListMember As String Dim sList As String Dim x As Integer Dim y As Integer Dim iCount As Integer myListMember = InputBox("Enter name of list member to be found") Set myNameSpace = Application.GetNamespace("MAPI") Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts) Set myFolderItems = myFolder.Items iCount = myFolderItems.Count sList = "" For x = 1 To iCount If TypeName(myFolderItems.Item(x)) = "DistListItem" Then Set myDistList = myFolderItems.Item(x) For y = 1 To myDistList.MemberCount If InStr(1, myDistList.GetMember(y).Name, myListMember) Then 'MsgBox myDistList.GetMember(y).Name & vbInformation, "Distribution List" If sList = "" Then sList = sList & myDistList.GetMember(y).Name & vbTab & myDistList.DLName Else sList = sList & vbCr & myDistList.GetMember(y).Name & vbTab & myDistList.DLName End If End If Next y End If Next x On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err Then Set wdApp = CreateObject("Word.Application") End If Set wdDoc = wdApp.Documents.Add wdApp.Visible = True wdApp.Activate With wdDoc.Range .InsertAfter sList .ParagraphFormat.TabStops.ClearAll .ParagraphFormat.TabStops.Add Position:=InchesToPoints(4), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces End With Set wdDoc = Nothing Set wdApp = Nothing End Sub
Set References
You need to go to Tools, References (in the VBA Editor) and add Word as a reference.
In this screenshot, it lists Word 14. If you use an older version of Word, you'll have a different version here.
This code seems to run but only displays an empty Word doc. I am running this script at work on a work computer. Thank you in advance.
Sweet! I was just about to start writing something like this.
No worries :)
It's Exchange 2010 SP3 I believe.
The path looks to be correct, other than for the folder "Research / Sales" it shows in the Msgbox as "Research %2F Sales", would this be likely to cause a problem or should it still work for that specific path?
Thanks!
That could be the problem. It's a "url escape code" and the path you are passing is looking for the /. Try using that format and see if it works.
OK it doesn't look like it's that - if I change the path to use the escape code it fails there with an "object not found" error. Switch it back to the "/" character and it goes straight past there until it fails at the "For y = 1 To myDistList.MemberCount" line again. On the plus side it does therefore seem to be locating the correct folder from the path at least!
Could there be anything that might prevent the macro from accessing the distributions/contact groups within the final folder? My AD account has full read/write access to those contact groups so I would hope not - what is that line attempting to do if you'll excuse my ignorance?
Really appreciate your help so far - many thanks :)
Thanks for the update. Yeah, if it’s getting to there, it’s failing on the dl. If you can open the dl and look at it, you should be able to get the count using a macro.
What it’s trying to do is count the number of contacts in the dl. Does the dl contain nested dls? That shouldn’t cause errors in counting since you are counting members, not contacts specifically. I’ll see if I can reproduce it but might not have a chance before Tuesday.
I didn't think that thru.... Sorry. Use your original path and msgbox myfolder.folderpath - I'm thinking the path isn't right and this will show if your path is correct.
Oh, what version of exchange do you use?
Hi - I managed to get this working for my own contacts folder, but when I change the myFolder variable to point to the public folders the macro fails at this point:
For y = 1 To myDistList.MemberCount
It's not a particularly helpful error message either:
Run-time error '-1040171003 (c2004005) The operation failed
Any help much appreciated!
what code are you using to reference the public folder? The mail folder is olPublicFoldersAllPublicFolders so you'd need to walk down it
set folder = Ns.GetDefaultFolder(olPublicFoldersAllPublicFolders).Folders("top level").folders("company contacts")
Hi Diane - I've used the following:
Set myFolder = myNameSpace.GetDefaultFolder(olPublicFoldersAllPublicFolders).Folders("Organisation (BeGo)").Folders("Research / Sales").Folders("Distribution Lists")
Thanks!
try Set myFolder = myNameSpace.GetDefaultFolder(olPublicFoldersAllPublicFolders)
msgbox myfolder
What does it give you for the path?
Thanks - it says "All Public Folders"
And if I then use my original path, it gives "Distribution Lists", which seems to be correct?
I tried using this but the word document that is created is empty. Any ideas as to what is going wrong?
Any error messages? comment out any on error resume next lines to see if it errors.
The line of code "If TypeName(myFolderItems.Item(x)) = "DistListItem" Then always responds negative, therefore no items are created for the list.
Any ideas?
Thanks for your help.
Martin
That line is looking for DLs and should only return negative if there are no dl's (contact groups).
This is fantastic ... I have been dreaming of this ... With that being said, we use a shared contact list that we add through public folders. As I am not a programmer by trade I am not sure which part of the code I should be changing so that it references the specific shared contacts folder and not my personal contacts. Any help/direction that you can provide would be great. Thank you.