共用方式為


將欄位新增至檢視

此範例示範如何使用 ViewFields 集合的 Add (String) 方法自定義檢視,以將字段新增至檢視。

範例

注意事項

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

您可以指定要在檢視中顯示哪些 Outlook 項目屬性,方法是將一或多個屬性新增至僅適用於 CardViewTableView 物件的 ViewFields 集合。 對於其他衍生的 View 物件,例如 BusinessCardViewCalendarViewIconViewTimelineView 物件,請使用其他方法來判斷檢視中顯示的 Outlook 項目屬性。 例如, 為 BusinessCardView 對象顯示的欄位是由電子名片 (EBC) 版面配置所決定,這些配置與每個顯示的 Outlook 專案相關聯。

若要取得檢視的 ViewFields 集合,請使用相關聯 View 物件的 ViewFields 屬性 (例如 CardViewTableView 物件) 。 ViewFields 集合的 Add 方法可用來建立 ViewField 物件,該物件代表要在檢視中顯示的 Outlook 項目屬性。 ViewField 對象不僅會識別要在檢視內顯示的 Outlook 項目屬性,也會描述該屬性的值應該如何顯示。 您可以修改 ViewField 物件的 ColumnFormat 屬性,以變更檢視中個別數據行屬性的顯示方式。

在下列程式代碼範例中,ModifyMeetingRequestsView 會取得 TableView 物件,代表來自使用者收件匣的所有檢視,也就是「會議邀請」檢視。 然後,此範例會使用 Add 方法,將 “Start” 和 “End” 字段新增至對應至 TableView 物件的 ViewFields 物件。 它也會將 [來源] 欄位的標籤變更為 [組織依據]。 ModifyMeetingRequestsView 接著會儲存修改過的 TableView 物件。

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 ModifyMeetingRequestsView()
{
    Outlook.TableView tableView = null;
    Outlook.ViewField startField = null;
    Outlook.ViewField endField = null;
    Outlook.ViewField fromField = null;
    try
    {
        tableView =
            Application.Session.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderInbox)
            .Views["Meeting Requests"] as Outlook.TableView;
    }
    catch { }
    if (tableView != null)
    {
        try
        {
            startField = tableView.ViewFields["Start"];
        }
        catch{}
        if (startField == null)
        {
            startField = tableView.ViewFields.Add("Start");
        }
        try
        {
            endField = tableView.ViewFields["End"];
        }
        catch{}
        if (endField == null)
        {
            endField = tableView.ViewFields.Add("End");
        }
        try
        {
            fromField = tableView.ViewFields["From"];
        }
        catch{}
        if (fromField != null)
        {
            fromField.ColumnFormat.Label = "Organized By";
        }
        try
        {
            tableView.Save();
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

另請參閱