共用方式為


取得並列舉選取的交談

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

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

The following managed code is written in C#. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio. 您應該使用 Office Developer Tools for Visual Studio) ,在 Outlook 增益集 (類別中使用下列程式碼 ThisAddIn 。 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱 MSDN 上的歡迎使用 Outlook 主要 Interop 元件參考

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 the current view is a table view. 
    if (inbox.CurrentView.ViewType == 
        Outlook.OlViewType.olTableView) 
    { 
        Outlook.TableView view = 
            inbox.CurrentView as Outlook.TableView; 
        // And check if the table view organizes conversations by date. 
        if (view.ShowConversationByDate == true) 
        { 
            Outlook.Selection selection = 
                Application.ActiveExplorer().Selection; 
            Debug.WriteLine("Selection.Count = " + selection.Count); 
             
             // Call GetSelection to create a Selection object 
            //  that includes any selected conversation header objects. 
            Outlook.Selection convHeaders = 
                selection.GetSelection( 
                Outlook.OlSelectionContents.olConversationHeaders) 
                as Outlook.Selection; 
            Debug.WriteLine("Selection.Count (ConversationHeaders) = "  
                + convHeaders.Count); 
 
            // Check if any conversation headers are selected. 
            if (convHeaders.Count >= 1) 
            { 
                foreach (Outlook.ConversationHeader convHeader in convHeaders) 
                { 
                    // Enumerate the items in each conversation header object. 
                    Outlook.SimpleItems items = convHeader.GetItems(); 
                    for (int i = 1; i <= items.Count; i++) 
                    { 
                        // Only enumerate 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); 
                        } 
                    } 
                } 
            } 
        } 
    } 
} 

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應