イントロダクション
この記事では、Microsoft Outlook 2002 オブジェクト ライブラリまたは Microsoft Office Outlook 2003 オブジェクト ライブラリを使用して、Microsoft Visual C# を使用して予定を取得する方法について説明します。
詳細情報
Outlook 2002 オブジェクト ライブラリまたは Outlook 2003 オブジェクト ライブラリを使用して Visual C# .NET プロジェクトの予定を取得するには、次の手順に従います。
Microsoft Visual Studio .NET または Microsoft Visual Studio 2005 で、新しいコンソール アプリケーション プロジェクトを作成します。
- [ファイル] メニューの [新規作成]をポイントし、 [プロジェクト]をクリックします。
-
プロジェクトの種類で、Visual C# プロジェクトを選択します。
注
Visual Studio 2005 で、[プロジェクトの種類] で [Visual C#] を選択します。
- [ Templates で、 Console Application を選択します。
-
[OK] を選択. 既定では、Class1.csという名前のファイルが作成されます。
注
Microsoft Visual C# 2005 では、Program.csは既定で作成されます。
Outlook 2002 オブジェクト ライブラリまたは Outlook 2003 オブジェクト ライブラリへの参照を追加します。 この手順を実行するには、以下のステップに従ってください。
[プロジェクト] メニューの [参照の追加] を選択します。
[ COM ] タブを選択します。
[COM タブで、Outlook 2003 を使用している場合Microsoft Outlook 11.0 オブジェクト ライブラリを選択するか、Outlook 2002 を使用している場合はMicrosoft Outlook 10.0 オブジェクト ライブラリを選択します。
選択
注
Visual Studio 2005 では、[ 選択] をクリックする必要はありません。
[ 参照の追加 ] ダイアログ ボックスで、[ OK] を選択します。
注
選択したライブラリのラッパーを生成するメッセージが表示されたら、[ はい] をクリックします。
Class1.cs コード ウィンドウで、既存のすべてのコードを次のコードに置き換えます。
using System; using System.Reflection; // to use Missing.Value //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line. //using Outlook = Microsoft.Office.Interop.Outlook; namespace RetrieveAppointment { /// <summary> /// Summary description for Class1. /// </summary> public class Class1 { /// <summary> /// The main entry point for the application. /// </summary> public static int Main(string[] args) { try { // Create the Outlook application. Outlook.Application oApp = new Outlook.Application(); // Get the NameSpace and Logon information. // Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi"); Outlook.NameSpace oNS = oApp.GetNamespace("mapi"); //Log on by using a dialog box to choose the profile. oNS.Logon(Missing.Value, Missing.Value, true, true); //Alternate logon method that uses a specific profile. // TODO: If you use this logon method, // change the profile name to an appropriate value. //oNS.Logon("YourValidProfile", Missing.Value, false, true); // Get the Calendar folder. Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); // Get the Items (Appointments) collection from the Calendar folder. Outlook.Items oItems = oCalendar.Items; // Get the first item. Outlook.AppointmentItem oAppt = (Outlook.AppointmentItem) oItems.GetFirst(); // Show some common properties. Console.WriteLine("Subject: " + oAppt.Subject); Console.WriteLine("Organizer: " + oAppt.Organizer); Console.WriteLine("Start: " + oAppt.Start.ToString()); Console.WriteLine("End: " + oAppt.End.ToString()); Console.WriteLine("Location: " + oAppt.Location); Console.WriteLine("Recurring: " + oAppt.IsRecurring); //Show the item to pause. oAppt.Display(true); // Done. Log off. oNS.Logoff(); // Clean up. oAppt = 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; } } }
このコードでは、"TO DO" コメントが表示される場所で必要な変更を行います。
F5 キーを押してビルドし、プログラムを実行します。 Outlook のセキュリティ機能では、予定情報が表示される前に追加のダイアログ ボックスが表示される場合があります。
リファレンス
詳細については、「 Visual Studio を使用した Microsoft Office 開発」を参照してください。