Eugene Astafiev

How To: Use Find and FindNext to retrieve Outlook Contact items

Not so long ago I described how to use the Find and FindNext methods to retrieve Outlook mail items from a folder. Today I am going to replace a mail item declaration with a contact declaration in the code. There is no difference between mail and contact items in case we use the Find and FindNext methods. To illustrate this I have modified the code a bit: replaced the MailItem class with the Contact one. And, of course, I changed the search criteria ;-)

The following code iterates over all contact items in the specified folder that has the CompanyName property set to “Add-in Express”. I look for my colleagues and output their first and last names to the debug window. You can use the DebugView utility to intercept the debug output.

C# and Add-in Express:

using System.Text;
using System.Diagnostics;
// ...
private void FindAllCoWorkers(Outlook.MAPIFolder folder)
{
    string searchCriteria = "[CompanyName] = \"Add-in Express\" ";
    StringBuilder strBuilder = null;
    int counter = default(int);
    Outlook._ContactItem contact = null;
    Outlook.Items folderItems = null;
    object resultItem = null;
    try
    {
        folderItems = folder.Items;
        if (folderItems.Count > 0)
        {
            strBuilder = new StringBuilder();
            resultItem = folderItems.Find(searchCriteria);
            while (resultItem != null)
            {
               if (resultItem is Outlook._ContactItem)
               {
                   counter++;
                   contact = resultItem as Outlook._ContactItem;
                   strBuilder.AppendLine("#" + counter.ToString() +
                                         "\tFirst Name: " + contact.FirstName+
                                         "\tLast Name: "+ contact.LastName);
               }
               Marshal.ReleaseComObject(resultItem);
               resultItem = folderItems.FindNext();
           }
           if (strBuilder != null)
               Debug.WriteLine(strBuilder.ToString());
       }
       else
          Debug.WriteLine("There is no match in the " +
                             folder.Name + " folder.");
   }
   catch (Exception ex)
   {
       System.Windows.Forms.MessageBox.Show(ex.Message);
   }
   finally
   {
      if (folderItems != null) Marshal.ReleaseComObject(folderItems);
   }
}

VB.NET and Add-in Express:

Imports System.Text
Imports System.Diagnostics
' ...
Private Sub FindAllCoWorkers(folder As Outlook.MAPIFolder)
    Dim searchCriteria As String = "[CompanyName] = ""Add-in Express"" "
    Dim strBuilder As StringBuilder = Nothing
    Dim counter As Integer = 0
    Dim contact As Outlook._ContactItem = Nothing
    Dim folderItems As Outlook.Items = Nothing
    Dim resultItem As Object = Nothing
    Try
        folderItems = folder.Items
        If (folderItems.Count > 0) Then
            strBuilder = New StringBuilder()
            resultItem = folderItems.Find(searchCriteria)
            While Not IsNothing(resultItem)
                If (TypeOf (resultItem) Is Outlook._ContactItem) Then
                   counter += 1
                   contact = resultItem
                   strBuilder.AppendLine("#" + counter.ToString() + _
                                         "\tFirst Name: " + contact.FirstName + _
                                         "\tLast Name: " + contact.LastName)
                End If
                Marshal.ReleaseComObject(resultItem)
                resultItem = folderItems.FindNext()
            End While
            If Not IsNothing(strBuilder) Then
                Debug.WriteLine(strBuilder.ToString())
            End If
        Else
            Debug.WriteLine("There is no match in the " + _
                              folder.Name + " folder.")
        End If
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(folderItems) Then Marshal.ReleaseComObject(folderItems)
    End Try
End Sub

C# and VSTO:

using System.Runtime.InteropServices;
using System.Diagnostics;
// ...
private void FindAllCoWorkers(Outlook.MAPIFolder folder)
{
    string searchCriteria = "[CompanyName] = \"Add-in Express\" ";
    StringBuilder strBuilder = null;
    int counter = default(int);
    Outlook._ContactItem contact = null;
    Outlook.Items folderItems = null;
    object resultItem = null;
    try
    {
        folderItems = folder.Items;
        if (folderItems.Count > 0)
        {
            strBuilder = new StringBuilder();
            resultItem = folderItems.Find(searchCriteria);
            while (resultItem != null)
            {
                if (resultItem is Outlook._ContactItem)
                {
                    counter++;
                    contact = resultItem as Outlook._ContactItem;
                    strBuilder.AppendLine("#" + counter.ToString() +
                                          "\tFirst Name: " + contact.FirstName +
                                          "\tLast Name: " + contact.LastName);
                }
                Marshal.ReleaseComObject(resultItem);
                resultItem = folderItems.FindNext();
           }
           if (strBuilder != null)
               Debug.WriteLine(strBuilder.ToString());
       }
       else
          Debug.WriteLine("There is no match in the "
                          + folder.Name + " folder.");
    }
    catch (Exception ex)
    {
       System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
       if (folderItems != null) Marshal.ReleaseComObject(folderItems);
    }
}

VB.NET and VSTO:

Imports System.Runtime.InteropServices
Imports System.Diagnostics
' ...
Private Sub FindAllCoWorkers(folder As Outlook.MAPIFolder)
    Dim searchCriteria As String = "[CompanyName] = ""Add-in Express"" "
    Dim strBuilder As StringBuilder = Nothing
    Dim counter As Integer = 0
    Dim contact As Outlook._ContactItem = Nothing
    Dim folderItems As Outlook.Items = Nothing
    Dim resultItem As Object = Nothing
    Try
        folderItems = folder.Items
        If (folderItems.Count > 0) Then
            strBuilder = New StringBuilder()
            resultItem = folderItems.Find(searchCriteria)
            While Not IsNothing(resultItem)
                If (TypeOf (resultItem) Is Outlook._ContactItem) Then
                    counter += 1
                    contact = resultItem
                    strBuilder.AppendLine("#" + counter.ToString() + _
                                          "\tFirst Name: " + contact.FirstName + _
                                          "\tLast Name: " + contact.LastName)
                End If
                Marshal.ReleaseComObject(resultItem)
                resultItem = folderItems.FindNext()
            End While
            If Not IsNothing(strBuilder) Then
                Debug.WriteLine(strBuilder.ToString())
            End If
        Else
            Debug.WriteLine("There is no match in the " + _
                               folder.Name + " folder.")
        End If
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(folderItems) Then Marshal.ReleaseComObject(folderItems)
    End Try
End Sub

See you on our forums and in the e-mail support!

2 Comments

  • http://1.gravatar.com/avatar/984f31a19668770a5cabbada199a5bd9?s=32&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Anand says:

    Hi,
    I have been trying to search the address book of Outlook programmtically. For that if i use “Item” method, search query only matches with the Display name where as i would like to search for an “Alias” in the Address book. In the dialogue box while searching address book, there is an option of “More Columns”. But i really don’t understand how that can be accesses programmatically. Could you please tell me how can i search for a particular “Alias” in address book.

  • Hi Anand,

    Please try to use the AdvancedSearch method of the Outlook Application class as shown in Advanced search in Outlook programmatically.

Post a comment

Have any questions? Ask us right now!