Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Sorting contacts

    In VBA/Word 2010, I want to get a list of the Contacts in my Outlook Contacts folder. The following code functions well, but the .Sort method doesn't properly sort by LastName. The Contacts sort by Last Name, A to W (last letter in my Contacts), and then there are about 10 names that follow, in ascending alpha sort. Is there something I've overlooked?

    Thanks.

    Code:
    Private Sub ListContacts()
    
        Dim oOutlookApp As Outlook.Application
        Dim oOutlookNS As Outlook.NameSpace
        Dim oContacts As Outlook.MAPIFolder
        Dim oContact As Outlook.ContactItem
    
        On Error Resume Next
    
        Set oOutlookApp = GetObject(, "Outlook.Application")
        If Err <> 0 Then
            Set oOutlookApp = CreateObject("Outlook.Application")
        End If
    
        Set oOutlookNS = oOutlookApp.GetNamespace("MAPI")
        Set oContacts = oOutlookNS.GetDefaultFolder(olFolderContacts)
        
        oContacts.Items.Sort "LastName"
      
        For Each oContact In oContacts.Items
            Debug.Print oContact.LastNameAndFirstName
        Next
    
    End Sub

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Richard,

    I would check those contacts for possible leading spaces and/or special characters. HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I think that a leading space would be obvious in the Immediate Window when Debug.Print creates the list. And wouldn't a leading space cause the items to appear before, not after, the letter A?

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    I found the solution to this. I needed a more explicit declaration of the Items in the Contacts collection:

    Code:
    Private Sub ListContacts()
        Dim oOutlookApp As Outlook.Application
        Dim oOutlookNS As Outlook.NameSpace
        Dim oContacts As Outlook.MAPIFolder
        Dim oContact As Outlook.ContactItem
        Dim oItems As Outlook.Items
        
        On Error Resume Next
    
        Set oOutlookApp = GetObject(, "Outlook.Application")
        If Err <> 0 Then
            Set oOutlookApp = CreateObject("Outlook.Application")
        End If
    
        Set oOutlookNS = oOutlookApp.GetNamespace("MAPI")
        Set oContacts = oOutlookNS.GetDefaultFolder(olFolderContacts)
        
        Set oItems = oContacts.Items
        
        oItems.Sort "LastName"
        
        For Each oContact In oItems
            Debug.Print oContact.LastName & ", " & oContact.FirstName
        Next
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •