More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 16-Apr-2014 08:03

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Login Password
Remember me
Register | Send my password
Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: Copy Distribution List to New Distribution List
Description: Subroutine will read an existing distro list within contacts, and copy it to a new distro list. I manage a large distro list for communications and found myself having to separate the original list into two lists. I did not want to manually enter them again, so I decided to automate the task, even if it took longer to automate than merely re-entering.
Date: 18-Nov-2004  10:23
Code level: beginner
Code area: Basic Outlook
Posted by: Gilbert Cedillo
This message is displayed as VB.NET
 Private Sub copyDistroList()

On Error GoTo errHandler

    Dim myOlApp As New Outlook.Application
    Dim myNameSpace As Outlook.NameSpace
    Dim myFolder As Outlook.MAPIFolder
    Dim myDistList As Outlook.DistListItem  'source distribution list contains all contacts
    Dim myDistList1 As Outlook.DistListItem 'target distribution list containing contacts A-L
    Dim myDistList2 As Outlook.DistListItem 'target distribution list containing contacts M-Z
    Dim myFolderItems As Outlook.Items
    Dim myRcpnt As Outlook.Recipient
    
    Dim intIterateContactItems As Integer   'track contact items
    Dim intIterateDLMemberItems As Integer  'track distribution members
    Dim intCountContactItems As Integer     'count contact items
    Dim intCountDLMemberItems As Integer    'count distribution members within list
    
    Dim strSourceDistList As String         'source distro list name container
    Dim strTargetDistList1 As String        'target 1 distro list name container
    Dim strTargetDistList2 As String        'target 2 distro list name container
    Dim blnListFound As Boolean             'track if source distro list was found
    
    strSourceDistList = "test"          'name of source distro list
    strTargetDistList1 = "test1"        'name of target 1 distro list
    strTargetDistList2 = "test2"        'name of target 2 distro list
    
    'create Outlook objects
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
    Set myFolderItems = myFolder.Items
    Set myDistList1 = myOlApp.CreateItem(olDistributionListItem)
    Set myDistList2 = myOlApp.CreateItem(olDistributionListItem)
    
    'intialize variables
    myDistList1.DLName = strTargetDistList1
    myDistList2.DLName = strTargetDistList2
    blnListFound = False
    
    'assign the count of all contact items to variable
    intCountContactItems = myFolderItems.Count
    
    'iterate through all Outlook contact items until the source distro list is located
    For intIterateContactItems = 1 To intCountContactItems
    
        'check to see if the contact item is a distribution list type
        If TypeName(myFolderItems.Item(intIterateContactItems)) = "DistListItem" Then
        
            'set the myDistList object as the DistListItem
            Set myDistList = myFolderItems.Item(intIterateContactItems)
            
            'check to see if the distro list is correct source list
            If myDistList.DLName = strSourceDistList Then
            
                'assign the distro list member count to variable
                intCountDLMemberItems = myDistList.MemberCount
                
                'iterate through all members of the distro list
                For intIterateDLMemberItems = 1 To intCountDLMemberItems
                
                    'get the member name, create the recipient, and assign to object variable
                    Set myRcpnt = myOlApp.Session.CreateRecipient(myDistList.GetMember(intIterateDLMemberItems).Address)
                    
                    'ensure the recipient can be resolved in the Exchange director
                    myRcpnt.Resolve
                    
                    'is recipient name resolved?
                    If myRcpnt.Resolved = True Then
                        
                        'get the first character of the recipient's last name (format in our Exchange server is Last, First)
                        Select Case Left(myDistList.GetMember(intIterateDLMemberItems).Name, 1)
                        'if the last name begins with letters between A and L, then add to 1st target distro list
                        Case "A" To "L"
                            myDistList1.AddMember myRcpnt
                        'if the last name begins with letters between M and Z, then add to 2nd target distro list
                        Case Else
                            myDistList2.AddMember myRcpnt
                        End Select
                        
                    'if recipient is not resolved then warn the user and move on to the next distro list member
                    Else
                        MsgBox "Sorry, I could not resolve the email address for " & _
                                myDistList.GetMember(intIterateDLMemberItems).Name & "." & _
                                vbCrLf & "Please write this information down and verify." & _
                                vbCrLf & "I will move on with the list.  Click okay " & _
                                "to continue.", vbOKOnly + vbCritical, "NOT RESOLVED"
                        Resume Next
                    End If

                    'Debug.Print myDistList.GetMember(intIterateDLMemberItems).Name & _
                                ", " & myDistList.GetMember(intIterateDLMemberItems).Address
                
                'get the next member of the distro list
                Next intIterateDLMemberItems
                
                'save the new distro lists
                myDistList1.Save
                myDistList2.Save
                'source distro list was found, so set variable to true
                blnListFound = True
                'since the source distro list is found, there is no need to continue with the contact items iteration
                Exit For
            End If
        End If
        
    'get the next item within Contacts
    Next intIterateContactItems
    
    'raise message box if source distro list was not found
    If blnListFound = False Then
        MsgBox "Your Source Distribution List was not found", vbOKOnly + vbInformation, "DISTRO LIST NOT FOUND"
    End If
    
setAllToNothing:
    'end of subroutine, so ensure all objects are emptied
    If Not myOlApp Is Nothing Then
        Set myOlApp = Nothing
    End If
    
    If Not myNameSpace Is Nothing Then
        Set myNameSpace = Nothing
    End If
    
    If Not myFolder Is Nothing Then
        Set myFolder = Nothing
    End If
    
    If Not myDistList Is Nothing Then
        Set myDistList = Nothing
    End If
    
    If Not myDistList1 Is Nothing Then
        Set myDistList1 = Nothing
    End If
    
    If Not myDistList2 Is Nothing Then
        Set myDistList2 = Nothing
    End If
    
    If Not myFolderItems Is Nothing Then
        Set myFolderItems = Nothing
    End If
    
    If Not myRcpnt Is Nothing Then
        Set myRcpnt = Nothing
    End If
    
    Exit Sub
    
errHandler:
    
    MsgBox Err.Number & " " & Err.Description, vbCritical + vbOKOnly, "ERROR"
    Resume setAllToNothing

End Sub
All 14comments
Page [ 1 2 Next >>  
  19-Nov-2004  09:00   
So this doesn't copy the list but splits it into two new lists, right?
  19-Nov-2004  15:12   
It does copy the original list, but it splits the results into two new lists, depending on last name. The code evaluates the first initial of the last name, then based on that, it will add the member to the appropriate new list. I just wanted to show others, who are experiencing the same challenges I am, how to get to the members within the distro list, and how to add them a distro list.
  24-Nov-2004  07:09   
I have a question here... what happens if a member itself is a distribution list? and to solve this, (say) we do it recursively drilling down this list again and adding the members, but finally say some list has our original list as a member, we would get into a cycle that never stops... and poof stack overflow.... what do you suggest to handle this?
  24-Nov-2004  19:08   
Hi Laks, I will test this on Monday when I am back in the office.
  29-Nov-2004  14:47   
In my example, if a member is a distribution list, it will not recursively drill down. When it resolves the name, it looks at the Exchange directory, not my personal contacts, so it pulls the contact name that most closely matches the name of the personal distribution list; thus pulling an incorrect contact name if you are copying a distribution list from your personal contacts. If the distribution list is created and managed on the Exchange server, then it will work just fine.
  30-Nov-2004  20:57   
To ensure you don't get into a cycle of rereading lists, putting everything into an Access table and then reading the table would work best, I think.

I'm also writing a system to manage many complex distro lists that have to be synchronized across two different system (during proof of concept phase). The distro lists have many nested distro lists. How do I add a distro list to a distro list? These are all in Public Folder, not on the Exchange server. In other words, DL_test contains jqpublic@msn.com and DL_test1 and DL_test2.? It has to be simple, but I don't see it anywhere in any examples. Please email me at dapiaoen@comcast.net.
  02-Dec-2004  08:55   
Create a recipient with the DL and add it with AddMember.
  13-Dec-2004  08:45   
If a recipient is not resolved the user is informed but the unresolved name isn't removed from the DL. If using the same principle for creating a recipient list for an email which is then Displayed, you still see the unresolved name. How can the name be removed if it does not resolve?
  15-Dec-2004  09:49   
Another question:

How to get/find/retrieve the _ContactItem that member of distribution list item is associated with?
  15-Dec-2004  09:53   
for(long i=1;i<=m_pContactFolder->Items->Count;i++)
{
   IDispatchPtr pItem=m_pContactFolder->Items->Item(i);
   CComQIPtr<_DistListItem> pDistListItem=pItem;
   if(pDistListItem!=NULL)
   {
       for(long j=1;j<=pDistListItem->MemberCount;j++)
       {
             RecipientPtr pRecip=pDistListItem->GetMember(j);
             //How to get the corresponding _ContactItem that is associated with current member
      }
  }
}
 
Page [ 1 2 Next >>