สรุป
บทความนี้อธิบายวิธีการใช้ไลบรารีวัตถุ Microsoft Outlook 10.0 เพื่อรับข้อความที่ระบุโดยใช้เมธอด ค้นหา และวิธีการ จํากัด ใน Microsoft Visual 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 เพื่อสร้างและเรียกใช้โปรแกรม
อ้างอิง
For more information about the security features of Outlook 2002, click the following article number to view the article in the Microsoft Knowledge Base:
290500 รายละเอียดเกี่ยวกับฟีเจอร์ความปลอดภัยของอีเมลที่เกี่ยวข้องกับนักพัฒนาใน Outlook 2002
ดูข้อมูลเพิ่มเติมเกี่ยวกับปัญหาที่อาจเกิดขึ้นเมื่อคุณใช้คุณสมบัติ Count ของคอลเลกชันขนาดใหญ่ ให้คลิกที่หมายเลขบทความต่อไปนี้เพื่อดูบทความในฐานความรู้ของ Microsoft:
294385 OL2002: คุณสมบัติ Count ไม่ถูกต้องโดยใช้การนัดหมายที่เป็นกิจวัตร