共用方式為


彙總檢視中搜尋及取得項目

此範例示範如何搜尋多個資料夾和存放區中的專案,以及在匯總數據表檢視中傳回這些專案。

範例

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

在下列程式代碼範例中, GetItemsInView 方法會檢查目前使用者的主要傳遞存放區帳戶是否為 Exchange 帳戶、目前使用者是否具有管理員,以及 立即搜尋 是否可在會話的預設存放區中運作。 因為搜尋依賴 Explorer 物件的 Search 方法,而且搜尋結果是使用 GetTable 方法取得,所以 GetItemsInView 會建立 Explorer 物件、在此物件中顯示 [收件匣],並使用它來設定搜尋。

GetItemsInView 會搜尋 MailItem 類型的所有資料夾,以尋找目前使用者管理員中標示為重要的專案。 在 GetItemsInView 呼叫 Search 方法之後,所有搜尋結果都會顯示在 [總管] 中,包括其他資料夾和符合搜尋準則的存放區專案。 GetItemsInView 會取得 TableView 物件,其中包含搜尋結果的這個總管檢視。 藉由使用這個 TableView 物件的 GetTable 方法,GetItemsInView 接著會取得 Table 物件,其中包含從搜尋傳回的匯總專案。 最後 ,GetItemsInView 會顯示 Table 物件中代表搜尋結果中專案的每個數據列的主旨數據行。

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 GetItemsInView()
{
    Outlook.AddressEntry currentUser =
        Application.Session.CurrentUser.AddressEntry;

    // Check whether the current user uses the Exchange Server.
    if (currentUser.Type == "EX")
    {
        Outlook.ExchangeUser manager =
            currentUser.GetExchangeUser().GetExchangeUserManager();

        // Check whether the current user has a manager.
        if (manager != null)
        {
            string managerName = manager.Name;

            // Check whether 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"]);
                }
            }
        }
    }
}

另請參閱