預設情況下,Microsoft Outlook 會依對話方式顯示收件匣中的項目。 如果使用者在收件匣內做了選擇,您可以使用程式取得該選擇,包括交談標題和交談項目。 本主題的程式碼範例會顯示如何取得收件匣內的選擇,並列舉該選擇中每一個交談的郵件項目。
範例包含一種方法,。 DemoConversationHeadersFromSelection 此方法將目前檢視設定為收件匣,然後檢查目前檢視是否為依日期排序的對話資料表。 要取得選取,包括任何選取的 ConversationHeader 物件,請DemoConversationHeadersFromSelection使用 Selection 物件的 GetSelection 方法,並將 OlSelectionContents.olConversationHeaders 常數指定為參數。 如果選擇了對話標頭,使用 DemoConversationHeadersFromSelectionSimpleItems 物件列舉每個所選對話中的項目,然後顯示這些對話項目的主旨,這些對話項目屬於 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. 你應該在 Outlook 外掛類別中使用以下程式碼 ThisAddIn , (使用 Office 開發者工具 for Visual Studio) 。 程式碼中的應用程式物件必須是由 ThisAddIn.Globals提供 的受信任 Outlook 應用程式物件。 欲了解更多使用 Outlook PIA 開發受管理 Outlook 解決方案的資訊,請參閱 MSDN 上的 「歡迎至 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 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 支援與意見反應。