摘要
本文介绍如何使用 Microsoft Visual Outlook 10.0 对象库中的 Find 方法和 Restrict 方法检索特定C#。
更多信息
若要使用 Microsoft Outlook 10.0 对象库检索特定消息,请执行以下步骤:
-
从 Microsoft Visual Studio .NET 或 Microsoft Visual Studio 2005 开始。
-
在"文件"菜单上,指向"新建",然后单击"Project"。
-
在" Visual C#项目类型"列表中 ,单击" 控制台应用程序"。
默认情况下,将创建 Class1.cs 文件。 注意 在 Microsoft Visual C# 2005 中,单击"Visual C#项目类型"列表中的" visual C#" 。 默认情况下,将创建 Program.cs。 -
添加对 Microsoft Outlook 10.0 对象库的引用。 为此,请执行以下步骤:
-
在"Project"菜单上,单击"添加引用"。
-
单击"COM"选项卡,找到"Microsoft Outlook 10.0 对象库",然后单击"选择"。
注意 在 Microsoft Visual C# 2005 中。 不需要单击"选择 "。 -
在" 添加引用" 对话框中,单击"确定 "。
-
如果系统提示为所选库生成包装器,请单击"是 "。
-
-
在代码窗口中,将代码替换为以下内容:
using System;
using System.Reflection; // to use Missing.Value namespace FilterAppointments { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [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("mapi"); // 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("YourValidProfile", 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("Total Items (unrestricted): " + oItems.Count); //Include all occurrences of recurring items, and then sort them. oItems.Sort ("[Start]", false); oItems.IncludeRecurrences = true; // Define the string for the search criteria. String sCriteria; // Set the criteria for the Date fields. sCriteria = "[Start] <= '09/01/2002 08:00 AM' and [End] >= '2/15/2002 08:00 PM'"; // Set the criteria for a string field. // sCriteria = "[Subject] = 'Weekly recurring meeting'"; // Set the criteria for a numeric field. // Look for meetings that have not been responded to. // sCriteria = "[ResponseStatus] = " + (Int32)Outlook.OlResponseStatus.olResponseNotResponded) // Use the Restrict method to reduce the number of items to process. Outlook.Items oRestrictedItems = oItems.Restrict(sCriteria); oRestrictedItems.Sort ("[Start]", false); oRestrictedItems.IncludeRecurrences = true; Console.WriteLine("Total Items Unrestricted : " + 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(" Subject: " + oAppt.Subject.ToString()); Console.WriteLine(" Start time: " + oAppt.Start.ToString()); Console.WriteLine(" End time: " + oAppt.End.ToString()); Console.WriteLine(" Occurrences: " + oAppt.RecurrenceState.ToString()); Console.WriteLine("\n\n"); oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetNext(); } // Use the Find method to get single match. sCriteria = "[Start] >= '09/30/2001'"; Console.WriteLine("Criteria: " + sCriteria.ToString()); oAppointment = (Outlook.AppointmentItem)oItems.Find(sCriteria); Console.WriteLine("Used Find with Date fields"); if (oAppointment is Outlook.AppointmentItem) Console.WriteLine("Found -> " + oAppointment.Subject.ToString()); else Console.WriteLine("No object found"); // 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("{0} Exception caught.", e); } //Default return value return 0; } } } -
在代码中搜索 TODO 文本字符串,然后修改环境的代码。
-
按 F5 键生成 和 以运行程序。
参考
有关 2002 年 Outlook 安全功能的信息,请单击以下文章编号以查看 Microsoft 知识库中的文章:
290500 Outlook 2002 中与开发人员相关的电子邮件安全功能的说明 有关使用大型集合的 Count 属性时可能会出现的问题的其他信息,请单击以下文章编号,在 Microsoft 知识库中查看文章:
294385 OL2002:使用定期约会的计数属性不正确