none
Accessing other contacts folders in Outlook - i.e. subfolders containing contacts RRS feed

  • Question

  • Hi

    Using following code to retrieve information from my Outlook contacts - but this is doing the main "default" Contacts folder - I also have several sub-folders under Contacts, how can I get it to include those as well?

    Many thanks

    Imports Microsoft.Office.Interop.Outlook
    Imports Microsoft.Office.Interop
    Public Class DemoForm
        WithEvents bsContacts As New BindingSource
        Private Sub DemoForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim MyList As New List(Of MyContact)
    
            Dim bResult As Boolean = False
            Dim OutlookApp As Outlook.Application = Nothing
            Dim olNameSpace As Outlook.NameSpace
            Dim olConFolder As Outlook.MAPIFolder = Nothing
            Dim olConItems As Outlook.Items = Nothing
            Dim olConItem As Outlook.ContactItem
    
            Try
                OutlookApp = New Outlook.Application
                olNameSpace = OutlookApp.GetNamespace("MAPI")
                '
                ' You may need to sign in, I have Outlook open while running this code
                '
                'oNameSpace.Logon( "", "", False, True)
    
                olConFolder = olNameSpace.GetDefaultFolder(
                    Outlook.OlDefaultFolders.olFolderContacts)
    
                olConItems = olConFolder.Items
    
    
                For x As Integer = 1 To olConItems.Count - 2
    
                    If TypeOf olConItems(x) Is ContactItem Then
                        Dim Item As ContactItem = CType(olConItems(x), ContactItem)
                        If Not String.IsNullOrEmpty(Item.Email1DisplayName) AndAlso Not String.IsNullOrEmpty(Item.Email1Address) Then
    
    
                            MyList.Add(New MyContact With
                                       {
                                           .ContactID = Item.EntryID,
                                           .DisplayName = Item.Email1DisplayName,
                                           .EmailAddress = Item.Email1Address
                                       }
                                   )
    
                        End If
                    End If
                Next
            Finally
                olConItem = Nothing
                olConItems = Nothing
                olConFolder = Nothing
                olNameSpace = Nothing
                OutlookApp = Nothing
            End Try
    
            bsContacts.DataSource = MyList
    
            ListBox1.DisplayMember = "DisplayName"
            ListBox1.DataSource = bsContacts
    
            TextBox1.DataBindings.Add("Text", bsContacts, "EmailAddress")
        End Sub
    End Class


    Darren Rose

    Saturday, September 27, 2014 5:32 PM

Answers

  • Hello Daren,

    You can use the Folders property of the Folder class. The property returns the Folders collection that represents all the folders contained in the specified Folder.  For example:

    Sub CreatePersonalContacts()  
     Dim myNamespace As Outlook.NameSpace  
     Dim myFolder As Outlook.Folder  
     Dim myNewFolder As Outlook.Folder 
     
     Set myNamespace = Application.GetNamespace("MAPI") 
     Set myFolder = myNamespace.GetDefaultFolder(olFolderContacts)  
     Set myNewFolder = myFolder.Folders.Add("My Personal Contacts")  
    End Sub

    Saturday, September 27, 2014 5:39 PM
  • Darren,

    You can use the following code to iterate over subfolders:

    For Each Folder In ContactsFolder.Folders
       Debug.Print Folder.Name
    Next
    

    The current forum is for developers, not to get a ready-made solution.  That is why I'd recommend reading the Getting Started with VBA in Outlook 2010 article in MSDN. Pay special attention to the Looping Through a Collection section.

    Sunday, September 28, 2014 1:03 PM
  • Turns out it was so much simpler to access a contact sub-folder than I thought (or the responses suggested)

    Simply changed

    olConFolder = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts

    to

    olConFolder = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Folders("No contact for 2 years+")


    Darren Rose

    • Marked as answer by wingers Wednesday, October 15, 2014 5:01 PM
    Wednesday, October 15, 2014 5:01 PM

All replies

  • Hello Daren,

    You can use the Folders property of the Folder class. The property returns the Folders collection that represents all the folders contained in the specified Folder.  For example:

    Sub CreatePersonalContacts()  
     Dim myNamespace As Outlook.NameSpace  
     Dim myFolder As Outlook.Folder  
     Dim myNewFolder As Outlook.Folder 
     
     Set myNamespace = Application.GetNamespace("MAPI") 
     Set myFolder = myNamespace.GetDefaultFolder(olFolderContacts)  
     Set myNewFolder = myFolder.Folders.Add("My Personal Contacts")  
    End Sub

    Saturday, September 27, 2014 5:39 PM
  • Thanks, but based on the code I posted, how can I modify my code to allow me to use a different contacts folder? tried modifying it like you showed but get error about cannot create folder

    Darren Rose

    Saturday, September 27, 2014 7:01 PM
  • It looks like you don't have sufficient permissions to create new folders there. Why do you need to create a subfolder? According to your initial post you just need to access it. Am I right?

    You can use the Folders property to get access to all subfolders.

    Saturday, September 27, 2014 7:20 PM
  • No I don't need to add a folder - I need to know how to modify my code in first post so it can do a different contacts folder rather than the main default one - I have several sub-folders under contacts and for example want to modify code I posted to either do the one called "supplier" or perhaps to do all my contacts folders

    Just need help to modify my existing code to do it


    Darren Rose

    Saturday, September 27, 2014 7:22 PM
  • Okay.

    >  tried modifying it like you showed but get error about cannot create folder

    Do you get the same error if you try to create a folder manually?

    Saturday, September 27, 2014 7:25 PM
  • Okay.

    >  tried modifying it like you showed but get error about cannot create folder

    Do you get the same error if you try to create a folder manually?


    I don't want to CREATE a folder??! I want to be able to use my code I posted to get contacts from a folder other than the main default contacts folder - as I say I have several sub-folders containing different contacts and I want to modify my code so it can look at the sub-folders as well or at least be able to look at a different folder other than the main one - nothing to do with adding or creating a folder

    Darren Rose

    Saturday, September 27, 2014 7:28 PM
  • Try to iterate over all folders returned from the Folders property.
    Saturday, September 27, 2014 10:43 PM
  • Try to iterate over all folders returned from the Folders property.
    okay that makes sense - but how, I am asking for some help in amending my existing code as posted above to do this - I am no expert, just trying to learn

    Darren Rose

    Saturday, September 27, 2014 11:24 PM
  • Darren,

    You can use the following code to iterate over subfolders:

    For Each Folder In ContactsFolder.Folders
       Debug.Print Folder.Name
    Next
    

    The current forum is for developers, not to get a ready-made solution.  That is why I'd recommend reading the Getting Started with VBA in Outlook 2010 article in MSDN. Pay special attention to the Looping Through a Collection section.

    Sunday, September 28, 2014 1:03 PM
  • Turns out it was so much simpler to access a contact sub-folder than I thought (or the responses suggested)

    Simply changed

    olConFolder = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts

    to

    olConFolder = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Folders("No contact for 2 years+")


    Darren Rose

    • Marked as answer by wingers Wednesday, October 15, 2014 5:01 PM
    Wednesday, October 15, 2014 5:01 PM