Outlook version of GetDistListMembers

March 13, 2009JPNo CommentsRate This Article


    Here is the Outlook version of the GetDistListMembers function. This one works from Outlook and doesn't trigger the OMG.

Function WriteDistListMembersToExcel(ListName As String) As Boolean
' adapted from http://www.slovaktech.com/code_samples.htm#DLToWord
' writes dist list members to a worksheet, one row for each contact in dist list

On Error GoTo ErrorHandler

' get reference to Outlook contacts folder
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olContactsFolder As Outlook.Items

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olContactsFolder = olNS.GetDefaultFolder(olFolderContacts).Items

' find specific dist list
Dim olDistList As Outlook.DistListItem
Set olDistList = olContactsFolder.item(ListName)

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 Outlook.Recipient
For i = 1 To lMemberCount
  ' no Object Model Guard!
 Set objRecip = olDistList.GetMember(i)
  tempVar(i, 1) = objRecip.Name
  tempVar(i, 2) = objRecip.Address
Next i

' get new Excel instance
Dim xlApp As Object ' Excel.Application
Dim xlBk As Object ' Excel.Workbook
Dim xlSht As Object ' Excel.Worksheet
Dim rngStart As Object ' Excel.Range
Dim rngHeader As Object ' Excel.Range

Set xlApp = GetExcelApp
If xlApp Is Nothing Then GoTo ExitProc

xlApp.ScreenUpdating = False

Set xlBk = xlApp.Workbooks.Add
Set xlSht = xlBk.Sheets(1)

' set up worksheet and write to range
xlSht.Name = ListName
Set rngStart = xlSht.Range("A1")
Set rngHeader = xlSht.Range(rngStart, rngStart.Offset(0, 1))

rngHeader.Value = Array("Name", "Email Address")

rngStart.Offset(1, 0).Resize(UBound(tempVar), 2).Value = tempVar

' if we got this far, assume success
WriteDistListMembersToExcel = True
xlApp.Visible = True
GoTo ExitProc

ErrorHandler:

ExitProc:
On Error Resume Next
Erase tempVar
Set objRecip = Nothing
Set olDistList = Nothing
Set olContactsFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
Set xlBk = Nothing
Set rngStart = Nothing
Set rngHeader = Nothing
Set xlApp = Nothing

End Function

Function GetExcelApp() As Object
' always create new instance
On Error Resume Next
  Set GetExcelApp = CreateObject("Excel.Application")
On Error GoTo 0
End Function

Usage:

Sub test()
Dim success As Boolean

If WriteDistListMembersToExcel("Managers") Then
  MsgBox "ok"
End If

End Sub

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 if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

5 Response(s) to Outlook version of GetDistListMembers ↓

  1. Lee Kennedy says:

    Can you tell me how I would go about exporting distribution lists held in the global address list, not just those saved in my contacts folder?

    Thanks

  2. JP says:

    Lee, I'll see what I can come up with.

  3. JP says:

    I posted another version of the code here:

    Extract GAL members to Excel

  4. Scott says:

    Hi Jimmy,

    Awesome site!! So far has saved me at least 10 hours manual work. In the reply to Lee, it looks like you may have a typo in the link. :)

    Keep up the great work!!
    Scott

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated July 26, 2010 @ 8:14 pm