Outlook Object Model : Using Restrict method to get contacts based on specific categories

Earlier i was working with one of the customer, where he wants to use Outlook Object Model to,

- retrieve the default contact folder items
- restrict on the retrieved items
- get the restricted items and gets its values – Full Name, Categories, FileAs… etc

Here is the way that i tried it – Code Snippet for your reference:

      Dim oOutlook As Outlook.Application
      Dim oNameSpace As Outlook.NameSpace
      Dim oFolder As Outlook.MAPIFolder
      Dim oContacts As Outlook.Items
      Dim oContact As Outlook.ContactItem
      Dim oItems As Outlook.Items
      Dim oItem As Outlook.ContactItem
      Dim i As Integer
      ' Connect with Outlook & get the Namespace
      Set oOutlook = New Outlook.Application
      Set oNameSpace = oOutlook.GetNamespace("MAPI")
      ' Retrieve the default Contacts Folder
      Set oFolder = oNameSpace.GetDefaultFolder(olFolderContacts)
      ' Retrieve the Items collection
      Set oContacts = oFolder.Items
      ' Create a Restriction on the Items
      Set oItems = oContacts.Restrict("[Categories] = 'Friend'")
      ' Display the number of items that match the restriction
      MsgBox "Found: " & oItems.Count
      ' Process each of the Restricted Items
      For Each oItem In oItems
         Debug.Print "Full Name : " & oItem.FullName & " Categories:  " & oItem.Categories & "File as:" & oItem.FileAs
      ' Clean it up
      Set oItems = Nothing
      Set oOutlook = Nothing

Happy Outlook programming!!

Skip to main content