Extract distribution list members to Excel

Ken Slovak has some code for extracting Outlook distribution lists to Word, so I went ahead and ported it to Excel.

The usual caveats apply; since you are accessing email addresses in Outlook from Excel, the OMG (Object Model Guard) will be triggered.

You specify the distribution list name as the parameter for the function. The name and email address of each distribution are added to a variant and returned to the calling sub.

Function GetDistListMembers(ListName As String) As Variant
' adapted from http://www.slovaktech.com/code_samples.htm#DLToWord
' returns a variant N rows high by 2 columns wide
' one row for each contact

' get reference to Outlook contacts folder
Dim olApp As Object ' Outlook.Application
Dim olNS As Object ' Outlook.Namespace
Dim olContactsFolder As Object ' Outlook.Items
Set olApp = GetOutlookApp

If olApp Is Nothing Then GoTo ExitProc

Set olNS = olApp.GetNamespace("MAPI")
Set olContactsFolder = olNS.GetDefaultFolder(10).Items ' olFolderContacts

' find specific dist list
Dim olDistList As Object ' Outlook.DistListItem

On Error Resume Next
Set olDistList = olContactsFolder.Item(ListName)
On Error GoTo 0

If olDistList Is Nothing Then GoTo ExitProc

' get count of dist list members
Dim lMemberCount As Long
lMemberCount = olDistList.MemberCount

' create temp variant and set size to one row for each contact
Dim tempVar As Variant
ReDim tempVar(1 To lMemberCount, 1 To 2)

' loop through dist list and extract members
Dim i As Long
Dim objRecip As Object ' Outlook.Recipient
For i = 1 To lMemberCount
  ' Object Model Guard
  Set objRecip = olDistList.GetMember(i)
  tempVar(i, 1) = objRecip.Name
  tempVar(i, 2) = objRecip.Address
Next i

GetDistListMembers = tempVar

ExitProc:
On Error Resume Next
Erase tempVar
Set objRecip = Nothing
Set olDistList = Nothing
Set olContactsFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
If bWeStartedOutlook Then
  olApp.Quit
End If
End Function

Function GetOutlookApp() As Object
On Error Resume Next
  Set GetOutlookApp = GetObject(, "Outlook.Application")
  If Err.Number <> 0 Then
    Set GetOutlookApp = CreateObject("Outlook.Application")
    bWeStartedOutlook = True
  End If
On Error GoTo 0
End Function

Sample Usage:

The following code takes a single distribution list and dumps the names and email addresses starting with cell A2:

Sub test()
Dim emails As Variant

With Range("A1")
  .Value = "Name"
  .Font.Bold = True
End With
With Range("B1")
  .Value = "E-mail Address"
  .Font.Bold = True
End With

emails = GetDistListMembers("My Dist List Name")

Range("A2").Resize(UBound(emails), 2).Value = emails

Range("A1").CurrentRegion.Columns.AutoFit

End Sub

And this version loops through an imaginary list of distribution list names in column A (starting from A2) and writes the members of each to the worksheet.

Sub test2()

Dim emails As Variant
Dim i As Long
Dim DistListToFind As String

With Range("D1")
  .Value = "Name"
  .Font.Bold = True
End With

With Range("E1")
  .Value = "E-mail Address"
  .Font.Bold = True
End With

' start loop at row 2
i = 2

Do While Not IsEmpty(Cells(i, 1))
  DistListToFind = Cells(i, 1).Value

  emails = GetDistListMembers(DistListToFind)

  Range("D" & Rows.Count).End(xlUp).Offset(1, 0). _
     Resize(UBound(emails), 2).Value = emails
  Range("D1").CurrentRegion.Columns.AutoFit

i = i + 1
Loop

End Sub

In a couple of days I'll post the Outlook version, which will have the added benefit of not triggering the OMG! I'll also work on a non-Office-specific version, so you could do this from any Office program with VBA (PowerPoint, for example).

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 8 Comments:

  1. vijiKaza writes:

    i need a batch file which will Extract the Members of a Distribution List to an Excel Worksheet. it would really be a great help if you send me . thanks. my mail id is vijayalakshmikaza@gmail.com thanks again.

  2. JP writes:

    I'm confused, doesn't the VBA code in this post do exactly that?

  3. chrisham writes:

    JP, I stuck this code in my Excel workbook (2007), and I get a debug error at

    " Range("D" & Rows.Count).End(xlUp).Offset(1, 0). _
    Resize(UBound(emails), 2).Value = emails"

    I am trying to extract two fields from my Outlook GAL list, viz, the ALIAS and the SMTP email ID.
    thanks,
    Hamilton

  4. chrisham writes:

    Okay, here's what I did
    in Sub Test()
    1) emails = GetDistListMembers("My Dist List Name") , here I changed ("List All Employees") .
    then
    at
    ' get count of dist list members
    Dim lMemberCount As Long
    lMemberCount = olDistList.MemberCount

    it errors out, indicating Runtime error 438, Object does'nt support this property or method.

    thanks for your patience.

  5. chrisham writes:

    JP, I had this code running on my office 2003 working fine, but somehow, in office 2007, the code runs and nothing is happens on the worksheets. In that, there's no desired output of all the GAL detail list.

    here's the link for the code:
    http://www.vbaexpress.com/kb/getarticle.php?kb_id=222

    Is it possible for you to see what is wrong with this??

Comments on this article are closed. Why?

Site last updated: February 12, 2012