Solved

Retrieve Distribution Lists from a List of Outlook Names

Posted on 2012-12-26
11
1,200 Views
Last Modified: 2012-12-28
This is a variation of the macro produced for question ID 27974272.
In that macro (see below), you enter several distribution lists in a spreadsheet and the output on the spreadsheet page is Member Names with the distribution lists.
For this macro, the Outlook Object Library has to be chosen with VBA, Tools, References.

In this new macro, I would like to be able to enter Member Names and have their distribution lists with the original member names produced.

Sub GetDistributionListMembers()
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
    Dim addrLists As AddressLists
    Set addrLists = objNamespace.Session.AddressLists
    Dim addrList As AddressList
    For Each addrList In addrLists
        If addrList.Name = "All Groups" Then
            Set myAddrList = addrList
            Exit For
        End If
    Next

idxLine = 1
ActiveSheet.Range("A1") = "Chosen Dist List"
ActiveSheet.Range("C1") = "Member name"
ActiveSheet.Range("D1") = "Dist List"

For Each Entry In myAddrList.AddressEntries
    If Entry.DisplayType = olDistList Then
        For Each c In Range("Chosen_Dist_List")
            If c.Value = Entry.Name Then
                For Each Mem In Entry.Members
                    If Mem.DisplayType = olUser Or Mem.DisplayType = olRemoteUser Then
                        ActiveSheet.Range("C" & idxLine + 1) = Mem.Name
                        ActiveSheet.Range("D" & idxLine + 1) = Entry.Name
                        idxLine = idxLine + 1
                    End If
                Next
            End If
        Next
    End If
Next
End Sub

Open in new window

0
Comment
Question by:Alex Campbell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 10

Expert Comment

by:tdlewis
ID: 38721671
I think this is what you're looking for:
Sub GetMemberDistributionLists()
    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Dim addrLists As AddressLists
    Set addrLists = objNamespace.Session.AddressLists
    Dim addrList As AddressList
    For Each addrList In addrLists
        If addrList.Name = "All Groups" Then
            Set myAddrList = addrList
            Exit For
        End If
    Next

    idxLine = 1
    ActiveSheet.Range("A1") = "Chosen Members"
    ActiveSheet.Range("C1") = "Member name"
    ActiveSheet.Range("D1") = "Dist List"
    
    For Each entry In myAddrList.AddressEntries
        If entry.DisplayType = olDistList Then
            For Each mem In entry.Members
                For Each c In Range("Chosen_Member_Names")
                    If (mem.DisplayType = olUser Or mem.DisplayType = olRemoteUser) _
                      And c.Value = mem.Name Then
                            ActiveSheet.Range("C" & idxLine + 1) = mem.Name
                            ActiveSheet.Range("D" & idxLine + 1) = entry.Name
                            idxLine = idxLine + 1
                    End If
                Next
            Next
        End If
    Next
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Alex Campbell
ID: 38721951
Thanks for the quick response time, but when I run it I get:

Run-time error '1004':

Method 'Range' of object '_Global' failed.
-----------------------------------------------------------
When I choose debug, the following line is highlighted:
  For Each c In Range("Chosen_Member_Names")
0
 
LVL 10

Expert Comment

by:tdlewis
ID: 38721998
Sorry. I should have mentioned something about that.  Your other subroutine relied on the named range "Chosen_Dist_List"; I wrote the new subroutine to use the named range "Chosen_Member_Names".

If you'd prefer another name or a different way to refer to the range where the member names are located, the line that's failing can easily be tweaked accordingly.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Alex Campbell
ID: 38722054
I have attached the spreadsheet that was used for the Getting the People for Lists.
Could you tweak it so it would work with your code for Getting the Lists for People.

Thanks,
Alex
GetLists.xlsm
0
 
LVL 10

Expert Comment

by:tdlewis
ID: 38722188
Updated spreadsheet is attached. The "Getting Lists for People" uses Tabelle2.

I also did a little cleanup to the subroutines:
put constants at the top of the file
all variables are explicitly defined
contents of columns C and D are removed before the results are generated
macro ends gracefully if it cannot find 'All Groups'
GetLists.xlsm
0
 
LVL 1

Author Comment

by:Alex Campbell
ID: 38722227
Thanks, I will take a look tomorrow. Have to leave right now.
0
 
LVL 1

Author Comment

by:Alex Campbell
ID: 38725108
I tried both tabs.

The first tab and macro works, but I get an error with the second one.

The error is:
Run-time error
'-590331899 (dcd04005)':

When I choose debug, I get "For Each mem In entry.Members" highlighted.

idxLine = 1
    For Each entry In myAddrList.AddressEntries
        If entry.DisplayType = desiredType Then
            For Each mem In entry.Members
                If (mem.DisplayType = olUser Or mem.DisplayType = olRemoteUser) Then
                    For Each c In Range(namedRangeMembers)
                        If c.Value = mem.Name Then
                            ActiveSheet.Range("C" & idxLine + 1) = mem.Name
                            ActiveSheet.Range("D" & idxLine + 1) = entry.Name
                            idxLine = idxLine + 1
                        End If
                    Next
                End If
            Next
        End If
    Next
End Sub
GetMembersAndLists.xlsm
0
 
LVL 10

Accepted Solution

by:
tdlewis earned 500 total points
ID: 38725473
In Excel, if your "Developer" tab is not showing, click on the Office button, choose Excel options, and then put a check mark next to "Show Developer tab in the ribbon".

Click on the Developer tab and then click on "Visual Basic Editor". From the Tools menu choose "References". Make sure that "Microsoft Office x.x Object Library" and "Microsoft Outlook x.x Object Library" are both checked (where x.x stands for the version number).
References dialogIf either is not checked, scroll down through the list until you find the right one. I think you'll probably find version 14.0 on your computer, but choose whatever version you have.
0
 
LVL 1

Author Comment

by:Alex Campbell
ID: 38726967
I think the problems may be time-outs or lock-outs from the network.
Since it is large corporate environment, I may have to go to the administrators for the information.  Thanks for the efforts. I will give you the points.

Double-checked. Yes, I do have them (see first image below)

I still get the error messages.

The first macro GetDistributionListMembers gets a result for one list in 15 seconds.
Then, at 30 seconds there is the message:
Outlook is trying retrieve data from the Microsoft Exchange Server Outlook.xxx.xxx

References
Error for first macro
Error for second macro
0
 
LVL 1

Author Closing Comment

by:Alex Campbell
ID: 38726970
Due to corporate restrictions, I was not able to get the information I was looking for, but the macro appears to be written properly.
0
 
LVL 10

Expert Comment

by:tdlewis
ID: 38727170
Try adding "On Error Resume Next" before the operations that are failing. That won't get you around the corporate restrictions, but it might allow you to generate the report for those distribution lists you are allowed to see.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question