Macro to pull members from distribution list.

santhoshbes2729

New Member
Joined
Oct 6, 2016
Messages
11
Hi All,

New to the Excel world. Need your help in providing me the excel macro to pull the member details from outlook distribution list. Can any one help me.

Thank You !!

Regards,
Santy
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,003
The following macro will first create a new worksheet, and then list the details of the specified distribution list in the newly created worksheet. Change the name of the distribution list accordingly. Note that the code needs to be placed in a regular module (Visual Basic Editor >> Insert >> Module).

Code:
Option Explicit

Sub PrintDistListDetails()


    Dim olApplication As Object
    Dim olNamespace As Object
    Dim olContactFolder As Object
    Dim olDistListItem As Object
    Dim destWorksheet As Worksheet
    Dim distListName As String
    Dim memberCount As Long
    Dim memberIndex As Long
    Dim rowIndex As Long
    
    Const olFolderContacts As Long = 10
    
    distListName = "MyDistListName" 'change the name accordingly


    Set olApplication = CreateObject("Outlook.Application")
    Set olNamespace = olApplication.GetNamespace("MAPI")
    Set olContactFolder = olNamespace.GetDefaultFolder(olFolderContacts)
    Set olDistListItem = olContactFolder.Items(distListName)
    
    Set destWorksheet = Worksheets.Add

    destWorksheet.Range("A1:B1").Value = Array("Name", "Address") 'column headers
    
    memberCount = olDistListItem.memberCount
    
    rowIndex = 2 'start the list at Row 2
    For memberIndex = 1 To memberCount
        With olDistListItem.GetMember(memberIndex)
            destWorksheet.Cells(rowIndex, "a").Value = .Name
            destWorksheet.Cells(rowIndex, "b").Value = .Address
        End With
        rowIndex = rowIndex + 1
    Next memberIndex
    
    destWorksheet.Columns.AutoFit
    
    Set olApplication = Nothing
    Set olNamespace = Nothing
    Set olContactFolder = Nothing
    Set olDistListItem = Nothing
    Set destWorksheet = Nothing
    
End Sub
Hope this helps!
 
Last edited:

santhoshbes2729

New Member
Joined
Oct 6, 2016
Messages
11
Hi Thanks for your reply.

Set olDistListItem = olContactFolder.Items(distListName)

gets a error "An object could not be found"
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,003
Did you change the name of the distribution list on this line to the name of your distribution list...

Code:
distListName = "MyDistListName" 'change the name accordingly
???
 

santhoshbes2729

New Member
Joined
Oct 6, 2016
Messages
11
No. I need to take it from global list.. If needed i can add it in the default folder.
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,003
With the code I offered, the distribution list needs to be within the default contacts folder.
 

santhoshbes2729

New Member
Joined
Oct 6, 2016
Messages
11
Hi, Thanks for your prompt reply.

I get error "[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]memberCount = olDistListItem.memberCount" for this as "Object does not support this property"

Can you pls help me in resolving this.

thank you !!
[/FONT]
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,003
When I tested the code, it ran successfully. I didn't get any errors. Try running it again, but this time when you get the error, click on Debug. This will highlight the line causing the error. Then, while still in debug mode, enter the following line in the Immediate Window (Ctrl+G) and press ENTER...

Code:
? typename(olDistListItem)
Does it return DistListItem?
 

Forum statistics

Threads
1,081,574
Messages
5,359,704
Members
400,545
Latest member
Damntheman30

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top