Share via


Obter e enumerar conversas selecionadas

Por padrão, o Microsoft Outlook exibe itens na caixa de entrada por conversa. Se um usuário marcar algum item na Caixa de Entrada, é possível ter acesso a essa seleção programaticamente, incluindo cabeçalhos de conversas e itens de conversa. O exemplo de código neste tópico mostra como ter acesso à seleção na Caixa de Entrada e enumerar os itens de email em cada conversa na seleção.

O exemplo contém um método, DemoConversationHeadersFromSelection. O método define a exibição atual para a caixa de entrada e verifica se o modo de exibição atual é uma exibição de tabela que exibe conversas classificadas por data. Para obter uma seleção, incluindo quaisquer objetos ConversationHeader selecionados, DemoConversationHeadersFromSelection use o método GetSelection do objeto Selection , especificando a constante OlSelectionContents.olConversationHeaders como um argumento. Se os cabeçalhos de conversa forem selecionados, DemoConversationHeadersFromSelection use o objeto SimpleItems para enumerar itens em cada conversa selecionada e, em seguida, exibirá o assunto desses itens de conversa que são objetos 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. Você deve usar o código a ThisAddIn seguir na classe de um suplemento do Outlook (usando ferramentas de desenvolvedor do Office para Visual Studio). The Application object in the code must be a trusted Outlook Application object provided by ThisAddIn.Globals. For more information about using the Outlook PIA to develop managed Outlook solutions, see the Outlook 2013 Primary Interop Assembly Reference on MSDN.

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); 
                        } 
                    } 
                } 
            } 
        } 
    } 
} 

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.