none
List Contacts in Each Contact Folder RRS feed

  • Question

  • Hi,

    When I open OUTLOOK 2013 and click on "People" I have a "My Contacts" and under it a folder named Contacts.  Today I added a new folder name "Test" under Contacts and created several new contacts in this folder.

    I am trying to create a VBA procedure to list all Contact folders and all contacts in each folder.   Here is a simplified version of the code:

    Sub GetContacts() Dim objOutlook As Outlook.Application Dim objAddressBook As Outlook.AddressList Dim objAddressEntry As Outlook.AddressEntry Dim lngRow As Long Set objOutlook = CreateObject("Outlook.Application") For Each objAddressBook In objOutlook.Session.AddressLists Debug.Print objAddressBook.Name
    'Msgbox objAddressBook.AddressEntries.Count

    For Each objAddressEntry In objAddressBook.AddressEntries Debug.Print objAddressEntry.Name Debug.Print objAddressEntry.Address Next Next End Sub

    Result:  Both contact folder names (Contacts and Test) are listed.  All contacts in the contact folder "Contacts" are listed, but none of the contacts in the contact folder "Test" are listed. 

    If I unset the Msgbox line the "Contact" folder count reports accurately 66 contacts.  However even though I clearly have several contacts defined in the "Test" folder the count returned is 0.

    What could I be doing wrong?  Thanks


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Wednesday, April 15, 2015 11:25 PM

Answers

  • Hi Greg Maxey,

    >>the address book is there, but the Address List Entries are not.  This sort of explains why the code isn't returning the Address List entries, but not why the address list entries are not in the book!<<

    Based on my test, if I created a contact without email address, the contact won't display in the Address book. Here is the figure for your reference:

    The AddressEntry object is an address in an AddressEntries object. Each AddressEntry object in the AddressEntries object holds information that represents a person, group,  or public folder to which the messaging system can deliver messages.

    Based on my understanding, if there is no email address for the contact, this is expected that there is no addressEntry for it.

    Please feel free to let me know if I misunderstood.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Greg Maxey Friday, April 17, 2015 12:14 PM
    Friday, April 17, 2015 11:54 AM
    Moderator

All replies

  • Hi Greg,

    Based on my understanding, the custom contact folder will not be added to the address book by default. If you want to add the custom folder to the address book, we can right click the folder and set the folder property like fingure below:

    Then we can used code above to get the custom contacts. Also we can get the custom contact by get the specific folder and here is sample for your reference:

    Sub PrintContacts()
    Dim aFolder As Folder
    Set aFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Folders("test")
    For Each aContact In aFolder.Items
        Debug.Print aContact.FirstName & ":" & aContact.Email1Address
    Next aContact
    End Sub

    You can get more detail about Address book from link below:

    Address book

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.


    Thursday, April 16, 2015 3:30 AM
    Moderator
  • Fei Xue,

    I had already attempted checking the box you show and it made no difference.  I even tried creating a folder named "A" that appeared above "Contacts" with the same result.  My code reported the three objAddressBooknames ("A, Contacts and Test), but only returned the objAddressListEntries for the Contacts list.

    Thanks for your code.  I've modified it as shown and it works for my purpose:

    Sub GetContacts()
    Dim objOutlook As Outlook.Application
    Dim objAddressBook As Outlook.AddressList
    Dim oItm As ContactItem
    Dim oFld As Object
      Set objOutlook = CreateObject("Outlook.Application")
      For Each objAddressBook In objOutlook.Session.AddressLists
        Debug.Print objAddressBook.Name
        Set oFld = objAddressBook.GetContactsFolder
        For Each oItm In oFld.Items
          Debug.Print oItm.FullName
        Next oItm
      Next
    lbl_Exit:
      Exit Sub
    End Sub

    It is still a mystery why the AddressListEntries are ignored in folders other than "Contacts" using my original code.Best Regards,

    Greg Maxey

    Whatever you are, be a good one.

    ~ Abraham Lincoln


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Thursday, April 16, 2015 12:56 PM
  • Hi Greg MAxey,

    Thanks sharing the code samlpe and let us know it works.

    >>I had already attempted checking the box you show and it made no difference.<<

    Would you mind checking the addressbook, whether it display in it like figure below:

    And did the code get the custom folder in the address book? 

    After the folder shows in the address book, the code get the contact successfully for me.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, April 17, 2015 1:46 AM
    Moderator
  • Fei,

    Now it is getting really weird.  In application I am trying to get access to the "Test" folder (now renamed "Custom Contacts" in a Word project.  When I use Application.GetAddress the following dialog is displayed:

    So yes, naturally I assumed that the contact "IS" showing up in the address book.  However, and I don't think I have ever used it, when I open the Address book in OUTLOOK, the address book is there, but the Address List Entries are not.  This sort of explains why the code isn't returning the Address List entries, but not why the address list entries are not in the book!



    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Friday, April 17, 2015 11:03 AM
  • So there is no doubt, the contact entry is there:


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Friday, April 17, 2015 11:04 AM
  • Hi Greg Maxey,

    >>the address book is there, but the Address List Entries are not.  This sort of explains why the code isn't returning the Address List entries, but not why the address list entries are not in the book!<<

    Based on my test, if I created a contact without email address, the contact won't display in the Address book. Here is the figure for your reference:

    The AddressEntry object is an address in an AddressEntries object. Each AddressEntry object in the AddressEntries object holds information that represents a person, group,  or public folder to which the messaging system can deliver messages.

    Based on my understanding, if there is no email address for the contact, this is expected that there is no addressEntry for it.

    Please feel free to let me know if I misunderstood.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Greg Maxey Friday, April 17, 2015 12:14 PM
    Friday, April 17, 2015 11:54 AM
    Moderator
  • Fei,

    That was it.  It seems that I just stumbled on this "anomaly" since in actual practice there will likely always be an e-mail address associated with each contact.

    I say "anomaly" as I'm not sure that I agree with your expectation.  It seems to me that since the "contact" is listed (without an e-mail address) in the Windows address book, perhaps a contact who I might want to call or write a letter to, that it would also be shown in the OUTLOOK address book.

    Regardless, thanks so much for your time and interest.  I now know why the code I first tried didn't seem to work and that the work around I'm now using will work regardless if there is a e-mail address or not.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Friday, April 17, 2015 12:14 PM