概要
この記事では、Microsoft Outlook 10.0 オブジェクト ライブラリを使用して、Microsoft Visual C# の Find メソッドと Restrict メソッドを使用して特定のメッセージを取得する方法について説明します。
詳細情報
Microsoft Outlook 10.0 オブジェクト ライブラリを使用して特定のメッセージを取得するには、次の手順に従います。
-
.NET Microsoft Visual Studioまたは 2005 Microsoft Visual Studio開始します。
-
[ファイル] メニューの [新規] をポイントし、[ファイルの選択] Project。
-
[プロジェクトの Visual C#] の一覧で 、[コンソール アプリケーション] をクリックします。
既定では、Class1.cs ファイルが作成されます。
注意 Microsoft Visual C# 2005 では、[プロジェクトの種類] のVisual C#の [Visual C# ] をクリック します。 既定では、Program.cs が作成されます。 -
Microsoft Outlook 10.0 オブジェクト ライブラリへの参照を追加します。 これを行うには、次の手順に従います。
-
[プロジェクト] メニューの [参照の追加] をクリックします。
-
[COM] タブをクリックし、[Microsoft Outlook 10.0 オブジェクト ライブラリ] を見つけて、[選択] をクリックします。
注意: Microsoft Visual C# 2005。 [選択] をクリックする 必要があります。 -
[参照 の追加] ダイアログ ボックスで 、[OK] を クリックします。
-
選択したライブラリのラッパーを生成するように求めるメッセージが表示されたら、[はい] をクリック します。
-
-
コード ウィンドウで、コードを次のコードに置き換えます。
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 キーを押してビルドし、プログラムを実行します。
参照情報
Outlook 2002 のセキュリティ機能の詳細については、次の記事番号をクリックして、Microsoft サポート技術情報の記事を参照してください。
290500 Outlook 2002
の開発者関連の電子メール セキュリティ機能の説明大きなコレクションの Count プロパティを使用するときに発生する可能性がある問題の詳細については、次の記事番号をクリックして、Microsoft サポート技術情報の記事を参照してください。
294385 OL2002: 定期的な予定を使用した誤ったカウント プロパティ