この例では、GetSelection(OlSelectionContents) メソッドを使用して、選択されたスレッドを取得して列挙します。
例
Microsoft Outlook では、受信トレイ内のアイテムをスレッド別に表示するように設定できます。 ユーザーが受信トレイで選択を行った場合、プログラムで選択項目を取得でき、これにはスレッドの見出しとアイテムも含まれます。 このトピックのコード例では、受信トレイ内の選択項目を取得し、選択されている各スレッドのメール アイテムを列挙する方法を示します。
この例には 1 つのメソッド DemoConversationHeadersFromSelectio が含まれます。 このメソッドは、現在のビューを受信トレイに設定した後、現在のビューがスレッドを日付順に表示している表ビューかどうかを調べます。 選択されているすべての ConversationHeader オブジェクトを含む選択項目を取得するために、DemoConversationHeadersFromSelection では Selection オブジェクトの GetSelection メソッドを使用し、引数として olConversationHeaders 定数を指定します。 スレッドの見出しが選択されている場合、DemoConversationHeadersFromSelection は SimpleItems オブジェクトを使用して、選択されている各スレッドのアイテムを列挙し、MailItem オブジェクトで表されるこれらのスレッド アイテムの件名を表示します。
Visual Studio を使用してこのコード例をテストする場合、Microsoft.Office.Interop.Outlook 名前空間をインポートするときに、まず Microsoft Outlook 15.0 オブジェクト ライブラリ コンポーネントへの参照を追加し、Outlook 変数を指定します。 using ステートメントは、コード例の関数の前に直接置くことはできません。パブリッククラス宣言の前に追加する必要があります。 次のコード行は、C# でインポートおよび割り当てを行う方法を示しています。
using Outlook = Microsoft.Office.Interop.Outlook;
private void DemoConversationHeadersFromSelection()
{
// Obtain Inbox.
Outlook.Folder inbox =
Application.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderInbox)
as Outlook.Folder;
// Set ActiveExplorer.CurrentFolder to Inbox.
// Inbox must be current folder.
Application.ActiveExplorer().CurrentFolder = inbox;
// Ensure that current view is TableView.
if (inbox.CurrentView.ViewType ==
Outlook.OlViewType.olTableView)
{
Outlook.TableView view =
inbox.CurrentView as Outlook.TableView;
if (view.ShowConversationByDate == true)
{
Outlook.Selection selection =
Application.ActiveExplorer().Selection;
Debug.WriteLine("Selection.Count = " + selection.Count);
// Call GetSelection to create a Selection object
// that contains ConversationHeader objects.
Outlook.Selection convHeaders =
selection.GetSelection(
Outlook.OlSelectionContents.olConversationHeaders)
as Outlook.Selection;
Debug.WriteLine("Selection.Count (ConversationHeaders) = "
+ convHeaders.Count);
if (convHeaders.Count >= 1)
{
foreach (Outlook.ConversationHeader convHeader in convHeaders)
{
// Enumerate the items in the ConversationHeader.
Outlook.SimpleItems items = convHeader.GetItems();
for (int i = 1; i <= items.Count; i++)
{
// Enumerate only MailItems in this example.
if (items[i] is Outlook.MailItem)
{
Outlook.MailItem mail =
items[i] as Outlook.MailItem;
Debug.WriteLine(mail.Subject
+ " Received:" + mail.ReceivedTime);
}
}
}
}
}
}
}