共用方式為


(Outlook) 在匯總檢視中搜尋及取得專案

TableView物件的GetTable方法可以傳回Table物件,其中包含來自相同存放區中一或多個資料夾的專案,或在匯總檢視中跨越多個存放區。 當您想要存取搜尋 (例如,搜尋存放區中的所有郵件項目) 所傳回的項目時,此方法特別有用。 本主題的範例將示範如何使用 [立即搜尋],來搜尋從已標示為重要之目前使用者的管理員接收到的所有項目,然後顯示每個搜尋結果的主旨。

下列程式碼範例包含 GetItemsInView 方法。 GetItemsInView首先進行一些檢查,以查看 Outlook 會話的目前使用者是否使用Microsoft Exchange Server、目前使用者是否有管理員,以及立即搜尋是否可在會話的預設存放區中運作。

由於最終搜尋依賴Explorer物件的Search方法,而最終結果顯示會使用GetTable方法,此方法是根據使用中檔案總管目前資料夾的目前檢視、 GetItemsInView 建立總管、在此總管中顯示 [收件匣],以及使用此Explorer物件設定搜尋。 GetItemsInView 將搜尋準則指定為目前使用者管理員的重要專案,並將搜尋範圍指定為 MailItem 專案類型的所有資料夾。

呼叫Explorer.Search方法之後 GetItemsInView ,任何搜尋結果都會顯示在此總管中,包括其他資料夾和存放區中符合搜尋準則的專案。 GetItemsInView 會取得 TableView 物件,其中包含搜尋結果的這個總管檢視。 使用這個TableView物件的GetTable方法, GetItemsInView 然後取得Table 物件,其中包含從搜尋傳回的匯總專案。 最後 GetItemsInView ,顯示 Table 物件的每個資料列的主旨資料行,代表搜尋結果中的專案。

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 解決方案的詳細資訊,請參閱 Outlook 主要 Interop 元件參考

private void GetItemsInView() 
{ 
    Outlook.AddressEntry currentUser = 
        Application.Session.CurrentUser.AddressEntry; 
 
    // Check if the current user uses the Exchange Server. 
    if (currentUser.Type == "EX") 
    { 
        Outlook.ExchangeUser manager = 
            currentUser.GetExchangeUser().GetExchangeUserManager(); 
 
        // Check if the current user has a manager. 
        if (manager != null) 
        { 
            string managerName = manager.Name; 
 
            // Check if Instant Search is enabled and operational in the default store. 
            if (Application.Session.DefaultStore.IsInstantSearchEnabled) 
            { 
                Outlook.Folder inbox = 
                    Application.Session.GetDefaultFolder( 
                    Outlook.OlDefaultFolders.olFolderInbox); 
 
                // Create a new explorer to display the Inbox as 
                // the current folder. 
                Outlook.Explorer explorer = 
                    Application.Explorers.Add(inbox, 
                    Outlook.OlFolderDisplayMode.olFolderDisplayNormal); 
 
                // Make the new explorer visible. 
                explorer.Display; 
 
                // Search for items from the manager marked important,
                // from all folders of the same item type as the current folder,
                // which is the MailItem item type. 
                string searchFor = 
                    "from:" + "\"" + managerName  
                    + "\"" + " importance:high"; 
                explorer.Search(searchFor, 
                    Outlook.OlSearchScope.olSearchScopeAllFolders); 
 
                // Any search results are displayed in that new explorer 
                // in an aggregated table view. 
                Outlook.TableView tableView =  
                    explorer.CurrentView as Outlook.TableView; 
 
                // Use GetTable of that table view to obtain items in that 
                // aggregated view in a Table object. 
                Outlook.Table table = tableView.GetTable(); 
                while (!table.EndOfTable) 
                { 
                    // Then display each row in the Table object 
                    // that represents an item in the search results. 
                    Outlook.Row nextRow = table.GetNextRow(); 
                    Debug.WriteLine(nextRow["Subject"]); 
                } 
            } 
        } 
    } 
} 

支援和意見反應

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