Microsoft KB Archive/310265

= How to retrieve specific messages by using the Find method and the Restrict method in Visual C# =

Article ID: 310265

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Outlook 2000 Standard Edition

-



This article was previously published under Q310265



SUMMARY
This article describes how to use the Microsoft Outlook 10.0 Object Library to retrieve specific messages by using the Find method and the Restrict method in Microsoft Visual C#.



MORE INFORMATION
To use the Microsoft Outlook 10.0 Object Library to retrieve specific messages, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In the Visual C# Projects types list, click Console Application.

By default, the Class1.cs file is created.

Note In Microsoft Visual C# 2005, click Visual C#&quot; in the Visual C# Projects types list. By default, Program.cs is created. Add a reference to the Microsoft Outlook 10.0 Object Library. To do so, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, locate Microsoft Outlook 10.0 Object Library, and then click Select.

Note In Microsoft Visual C# 2005. you do not have to click Select. In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the code window, replace the code with the following: using System; using System.Reflection;    // to use Missing.Value

namespace FilterAppointments {  ///    /// Summary description for Class1. ///   class Class1 {     ///       /// The main entry point for the application. ///      [STAThread] public static int Main(string[] args) {        try {           // Create an Outlook application. Outlook.Application oApp = new Outlook.Application; // Get the Mapi NameSpace and the Logon. Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace(&quot;mapi&quot;); // Log on using dialog to choose a profile. oNS.Logon(Missing.Value, Missing.Value, true, true);

// Alternate Logon using specific profile // TODO: Change the profile name where it is appropriate. //oNS.Logon(&quot;YourValidProfile&quot;, Missing.Value, false, true); // Get the Calendar folder. Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

// Get the Items collection in the folder. Outlook.Items oItems = (Outlook.Items)oCalendar.Items; Console.WriteLine(&quot;Total Items (unrestricted): &quot; + oItems.Count);

//Include all occurrences of recurring items, and then sort them. oItems.Sort (&quot;[Start]&quot;, false); oItems.IncludeRecurrences = true;

// Define the string for the search criteria. String sCriteria;

// Set the criteria for the Date fields. sCriteria = &quot;[Start] <= '09/01/2002 08:00 AM' and [End] >= '2/15/2002 08:00 PM'&quot;;

// Set the criteria for a string field. // sCriteria = &quot;[Subject] = 'Weekly recurring meeting'&quot;;

// Set the criteria for a numeric field. // Look for meetings that have not been responded to. // sCriteria = &quot;[ResponseStatus] = &quot; + (Int32)Outlook.OlResponseStatus.olResponseNotResponded)

// Use the Restrict method to reduce the number of items to process. Outlook.Items oRestrictedItems = oItems.Restrict(sCriteria); oRestrictedItems.Sort (&quot;[Start]&quot;, false); oRestrictedItems.IncludeRecurrences = true;

Console.WriteLine(&quot;Total Items Unrestricted : &quot; + oRestrictedItems.Count);

Outlook.AppointmentItem oAppointment;

//Get each item until item is null. Outlook.AppointmentItem oAppt; oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetFirst; while (oAppt != null) {              // Console.WriteLine(i.ToString); Console.WriteLine(&quot; Subject: &quot; + oAppt.Subject.ToString); Console.WriteLine(&quot; Start time: &quot; + oAppt.Start.ToString); Console.WriteLine(&quot; End time: &quot; + oAppt.End.ToString); Console.WriteLine(&quot; Occurrences: &quot; + oAppt.RecurrenceState.ToString); Console.WriteLine(&quot;\n\n&quot;); oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetNext; }

// Use the Find method to get single match. sCriteria = &quot;[Start] >= '09/30/2001'&quot;; Console.WriteLine(&quot;Criteria: &quot; + sCriteria.ToString); oAppointment = (Outlook.AppointmentItem)oItems.Find(sCriteria); Console.WriteLine(&quot;Used Find with Date fields&quot;); if (oAppointment is Outlook.AppointmentItem) Console.WriteLine(&quot;Found -> &quot; + oAppointment.Subject.ToString); else Console.WriteLine(&quot;No object found&quot;);

// Log off oNS.Logoff;

// Clean up           oAppointment = null; oRestrictedItems = null; oItems = null; oCalendar = null; oNS = null; oApp = null; }           //Simple error handling catch (Exception e)        { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }

//Default return value return 0; }  } }                    </li> Search for the TODO text string in the code, and then modify the code for your environment.</li> Press the F5 key to build and to run the program.</li></ol>

<div class="references_section">