摘要
本文將說明如何使用 Microsoft Outlook 10.0 物件程式庫使用 Find 方法及 Microsoft Visual C 中的限制方法來取回特定郵件#。
其他相關資訊
若要使用 Microsoft Outlook 10.0 物件程式庫來取回特定郵件,請遵循下列步驟:
-
請Microsoft Visual Studio .NET 或 Microsoft Visual Studio 2005。
-
在 [檔案>功能表上,指向 [新增,然後按一下 [Project。
-
在 Visual C# Projects 類型 清單中,按一下 [ 主控台應用程式。
根據預設,會建立 Class1.cs 檔案。
注意 在 Microsoft Visual C# 2005 中,按一下 Visual C# Projects 類型清單中的 [Visual C#」 。 根據預設,Program.cs 會建立。 -
新增 Microsoft 10.0 Outlook文件庫的參照。 若要這麼做,請遵循下列步驟:
-
在[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:使用週期性約會的 Count 屬性不正確