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!

4 Comments

Post a comment

Have any questions? Ask us right now!