共用方式為


取得並列舉選取的交談

此範例會使用 GetSelection (OlSelectionContents) 方法來取得並列舉選取的交談。

範例

Microsoft Outlook 可以設定為依交談顯示收件匣中的專案。 如果使用者在收件匣內做了選擇,您可以使用程式取得該選擇,包括交談標題和交談項目。 本主題的程式碼範例會顯示如何取得收件匣內的選擇,並列舉該選擇中每一個交談的郵件項目。

此範例包含一個方法,即 DemoConversationHeadersFromSelection。 方法會將目前的檢視設定為 [收件匣],然後檢查目前的檢視是否為顯示依日期排序之交談的數據表檢視。 為了取得選取範圍,包括任何選取的 ConversationHeader 物件,DemoConversationHeadersFromSelection 會使用 Selection 物件的 GetSelection 方法,將 olConversationHeaders 常數指定為自變數。 如果已選取交談標頭,DemoConversationHeadersFromSelection 會使用 SimpleItems 對象來列舉每個選取交談中的專案,然後顯示這些交談專案為 MailItem 物件的主旨。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in 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);
                        }
                    }
                }
            }
        }
    }
}

另請參閱