共用方式為


使用 SetColumns 有效率地列舉資料夾中的專案

此範例示範如何使用 SetColumns (String) 方法來快取集合中每個專案的特定屬性,以改善列舉 Items 集合的效能。

範例

注意事項

下列程式代碼範例是 Microsoft Office Outlook 2007 程式設計應用程式的摘錄。

若要列舉集合中的專案,請使用 SetColumns 方法快取 Items 集合上的屬性。 SetColumns 會採用代表屬性名稱的逗號分隔字串自變數。 列舉集合中的所有項目之後,請呼叫 ResetColumns () 方法來清除屬性快取。

在下列程式代碼範例中,EnumerateContactsWithSetColumns 會使用 SetColumns 方法來快取 [連絡人] 資料夾中專案的 FileAsCompanyNameJobTitle 屬性。 請注意,您必須在限制中測試空字串或 Null 參考。

請注意,Outlook 資料夾可能包含不同類型的專案。 此程式代碼範例會使用建立協助 程序類別以存取一般 Outlook 專案成員中定義的 OutlookItem 協助程式類別,以方便地呼叫 OutlookItem.Class 屬性,在假設專案是聯繫人專案之前,先驗證資料夾中已篩選專案子集中每個專案的郵件類別。

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 EnumerateContactsWithSetColumns()
{
    // Obtain Contacts folder
    Outlook.Folder folder =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderContacts)
        as Outlook.Folder;
    string filter = "Not([CompanyName] Is Null)" +
        " AND Not([JobTitle] Is Null)";
    Outlook.Items items = folder.Items.Restrict(filter);
    items.SetColumns("FileAs, CompanyName, JobTitle");
    for (int i = 1; i <= items.Count; i++)
    {
        // Create an instance of OutlookItem
        OutlookItem myItem = new OutlookItem(items[i]);
        if (myItem.Class == Outlook.OlObjectClass.olContact)
        {
            // Use InnerObject to return ContactItem
            Outlook.ContactItem contact =
                myItem.InnerObject as Outlook.ContactItem;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(contact.FileAs);
            sb.AppendLine(contact.CompanyName);
            sb.AppendLine(contact.JobTitle);
            sb.AppendLine();
            Debug.WriteLine(sb.ToString());
        }
    }
    items.ResetColumns();
}

另請參閱