Create a view

This example shows how to use the Add(String, OlViewType, OlViewSaveOption) method of the Views collection to create a view for a Folder object.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

You can create customizable views that allow you to sort, group, and view data of all different types within the View Pane of the Outlook explorer window. You can also customize built-in views programmatically. The following table lists objects that represent Outlook views.

Object Name

Description

BusinessCardView

Data is viewed as a series of electronic business card images.

CalendarView

Data is viewed in a calendar format.

CardView

Data is viewed in a series of cards.

IconView

Data is viewed as Windows folder icons or explorer icons.

TableView

Data is viewed in a simple field-based table.

TimelineView

Data is viewed in a customizable linear time line.

You can access properties and methods that are common to all views by using the View object. However, to access certain properties that are not common to all views, you must cast the View object to a derived View object that the property you want to access belongs to. For example, to access the HeadingsFont property of the Cardview object, cast the View object to the Cardview object. If you want to determine which type of view is represented by a particular View object, use the ViewType property.

To create a new view, use the Add method of the Views collection for a Folder object. Then set the visibility for the view either at the time of creation, or at any time after the view is created. To set the visibility for the view at the time of creation, specify an OlViewSaveOption constant in the SaveOption parameter of the Add method. To set the visibility at any time after the view is created, specify an OlViewSaveOption constant for the SaveOption property of the View object.

Adding a new view raises the ViewAdd event of the Views collection. Once the view is created, customize the view programmatically by casting the View object to one of the derived objects and then making necessary changes. Use the Save method of the derived View object or the View object to save any changes to the view. Finally, use the Apply method of the derived View object or the View object to apply the view to the current Explorer object. This raises the ViewSwitch event of the Explorer object.

In the following code example, CreateMeetingRequestsView adds a new view named “Meeting Requests” to the user’s Inbox by casting the View object to a TableView object. CreateMeetingRequestsView then calls the Add method of the Views object with the Name parameter set to “Meeting Requests” and the ViewType parameter set to olTableView. The Filter property of the TableView object is set to a DAV Searching and Locating (DASL) string that causes the view to display only when there are items that contain “IPM.Schedule” in the message class for the item. The new view is then saved and applied.

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 CreateMeetingRequestsView()
{
    const string PR_MESSAGE_CLASS =
        "http://schemas.microsoft.com/mapi/proptag/0x001A001E";
    Outlook.Views views =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderInbox).Views;
    Outlook.TableView tableView = (Outlook.TableView)
        views.Add("Meeting Requests",
        Outlook.OlViewType.olTableView,
        Outlook.OlViewSaveOption.olViewSaveOptionThisFolderEveryone);
    tableView.Filter = "\"" + PR_MESSAGE_CLASS + "\"" +
        " like 'IPM.Schedule%'";
    tableView.Save();
    tableView.Apply();
}

See also