Creating Custom Views

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

 

Morley Tooke
Microsoft Corporation

April 2001

Applies to:
   Microsoft® Outlook® 2002

Summary: Microsoft Outlook 2002 allows you to create and customize views. Views allow you to better sort, group and view Outlook items of all different types. Views are defined using Extensible Markup Language (XML) schemas that allow views to be further customized and fully rendered by Outlook. You can modify an instance of the schema by using the View object's XML property. This article provides a conceptual overview (complete with working code samples) of creating and customizing views programmatically in Outlook. (9 printed pages)

Contents

Introduction View Types Overview Creating a View Removing Views Customizing Views Conclusion

Introduction

Microsoft Outlook allows you to manage and display a variety of different types of data. Whether it's e-mail, Notes, Tasks or a Calendar, Outlook displays information in a clean and effective fashion. But imagine having the ability to customize the look and feel of your interface based on the type of data you are viewing or the folder in which you are viewing it.

Outlook 2002 lets you create and customize your very own views. Views allow you to organize and display certain types of data based on a predefined schema. For example, instead of viewing new Inbox e-mail messages in the standard table format, you could view the messages as points along a linear timeline. You could even customize the font, the number of columns, the size of the text, or even the amount of information displayed on the screen.

Because views are rendered using an Extensible Markup Language (XML) schema, creating and customizing views couldn't be easier.

Regardless of how you use Outlook, the added flexibility and functionality that views provide is a tremendous asset. They allow you to group, organize, customize and display only the data that you want to view.

This article discusses and provides examples of how to create, remove, and customize views.

While this article describes how to modify an instance of a view's schema using the Microsoft XML (MSXML) parser, a discussion of the MSXML parser is largely outside the scope of this article. For more information about using the MSXML parser, see the Microsoft XML Software Development Kit (SDK) Documentation on MSDN online.

View Types Overview

Views allow you to customize the way you view data, regardless of the datatype. Views can be created based on a standard view type and customized by using the MSXML parser to modify the instance of the XML schema of the view.

Outlook features five different default view types. Each view schema is designed to display information in a clear and concise format, but also to be flexible enough to be customized to meet the needs of a specific set of data.

The five view types are as follows:

Table 1. Outlook view types

View type OlViewType enumerated constant value Description
Table view olTableView The standard view type used for viewing messages in the Inbox. The Table view consists of customizable rows and columns.
Calendar view olCalendarView Allows you to view data in a Calendar format. This is useful for organizing your data in Day, Week or Month view. Message headers appear within a calendar grid.
Timeline view olTimelineView Allows you to view data as it is received by using a linear time line. The timeline view allows you to track items as they occur over the course of a period of time. This is useful for viewing time critical information such as tasks or meetings.
Icon view olIconView Allows you to view data as icons, similar to a Microsoft Windows® folder or Windows Explorer. 
Card view olCardView Allows you to view data as a series of cards. Each card displays the information contained by the item and can be sorted. This is useful for viewing personal information such as contact information.

Before you create a view, you should take time to consider the factors that determine how best to display your data. Much of this is related directly to the type of data you are trying to display. For instance, when displaying time-critical tasks in the Tasks folder, it may be advantageous to display each item on a liner timeline. Hence, the Timeline view may be the best choice for this view. Similarly, you might consider the Card view type when displaying the contents of the Notes folder.

Along with the type of data being displayed, you should also consider the type of user using the view, what the view will be used for, and any other factors that might govern the overall effectiveness of the view itself.

Creating a View

Once you decide what type of view will best suit your needs, you can create your new view. Use the Views collection's Add method to create a view.

The Add method accepts four arguments. The Name argument is a string that specifies the name of the new view. The ViewType argument accepts an enumerated constant of type OlViewType that specifies the type of the new view (see Table 1 for a list of enumerated constants). Finally, the SaveOption argument accepts an enumerated constant of type OlViewSaveOption that specifies the options associated with accessing the view.

The OlViewSaveOption enumerated constants are as follows:

Table 2. OlViewSaveOption enumerated constants

OlViewSaveOption enumerated constant value Description
olViewSaveOptionAllFoldersOfType The view can be accessed in all folders of this type.
olViewSaveOptionThisFolderEveryOne All users can access the view. This view is available to this folder only.
olViewSaveOptionThisFolderOnlyMe The view can be accessed in this folder only. Only the current user can access this view.

The following example creates a new view. The function accepts the view's name, location, and type as arguments, and creates a new view that is available in the specified folder to all users.

Function CreateView(ByVal strName As String, ByVal cnstFolderName As OlDefaultFolders, _
                    ByVal cnstViewType As OlViewType) As View
    'If succesful, returns a new view.

    Dim olApp As Outlook.Application
    Dim objName As NameSpace
    Dim objViews As Views
    
    On Error Resume Next
    
    Set olApp = Outlook.Application
    Set objName = olApp.GetNamespace(Type:="MAPI")
    Set objViews =  objName.GetDefaultFolder(FolderType:=cnstFolderName).Views
    
    'Create the new view.
    Set CreateView = objViews.Add(Name:=strName, _
                     ViewType:=cnstViewType, _
                     SaveOption:=olViewSaveOptionThisFolderEveryone)
    
End Function

The above example uses defensive programming to avoid failing. In this case, the line On Error Resume Next is included in case the Add method fails. Because the Add method is within a function and accepts parameters as arguments, it's a good idea to use error trapping. The Add method can fail if either an enumerated constant is incorrect or if the name given to the new view is already in use by an existing view.

Use the following code to call the above function.

Sub ViewDriver()
'Calls the CreateView function to return a view.

    Dim objView As View
    
    Set objView = CreateView(strName:="Main Inbox Table", _
                              cnstFolderName:=olFolderInbox, _
                              cnstViewType:=olTableView)
                              
    If objView Is Nothing Then
        MsgBox "The view was not created."
    Else
        MsgBox "The view: " & objView.Name & " was created sucessfully."
    End If

End Sub

The above subroutine calls the CreateView function and displays a message based on the outcome of the method. If the Add method fails, the function will return an empty object.

Removing Views

To remove a view, use the Remove method of the Views collection. The following example accepts the name of a view as a string as well as the folder in which it exists, and removes the view from the Views collection if it exists. If the view cannot be found, the function returns False and a message is displayed to the user.

Function RemoveView(ByVal strName As String, ByVal cnstFolderName As OlDefaultFolders) As Boolean
'If succesful, removes a view.

    Dim olApp As Outlook.Application
    Dim objName As NameSpace
    Dim objView As View
    Dim objViews As Views

    'Continue if error occurs.
    'On Error continue

    Set olApp = Outlook.Application
    Set objName = olApp.GetNamespace(Type:="MAPI")
    Set objViews = objName.GetDefaultFolder(FolderType:=cnstFolderName).Views
    RemoveView = False

    For Each objView In objViews
        If objView.Name = strName Then
            objViews.Remove Index:= strName
            RemoveView = True
        End If
    Next objView

End Function

Use the following subroutine to call the above function.

Sub CallRemoveView()
    If RemoveView(strName:="New Table", cnstFolderName:=olFolderInbox) Then
        MsgBox "The view was removed."
    Else
        MsgBox "The view was not removed."
    End If
End Sub

Customizing Views

Because each view is also defined using XML, you can customize views using the Microsoft XML (MSXML) parser. Customizing views allows you to further modify the interface to meet the needs of your audience.

Each view is defined as an XML schema. The schema contains the XML definition and value for every setting or property contained in the view. For example, a Table view schema contains the style settings that define the font and font color of the view as well as the make up and position of columns within the table itself. When the view is applied to a folder, it is rendered based on the settings defined in the XML schema. By modifying the settings defined in the schema, you can further customize the view itself.

While this article describes how to programmatically modify and customize a view, much of that is outside of the scope of this article. For more information on the default XML schemas see XML View Definitions. For more information on using the MSXML parser see the MSDN Online XML Developer Center.

The following example creates a new view and modifies its properties. The subroutine accepts a string representing the name of the new view as well as string values representing the property and associated value to modify. This subroutine can accept either one or two property-value pairs. This is accomplished using the Optional keyword in the parameter line of the subroutine.

Sub SetTableProperties(ByVal strViewName As String, ByVal strPropA As String, _
                        ByVal strValA As String, Optional ByVal strPropB As String, _
                        Optional ByVal strValB As String)
    'Creates a new view and modifies its properties.
    'Accepts 1 or 2 element names and their values.
    'The second element and value are optional.

    Dim objViews As Views
    Dim objView As View
    'Set a reference to the MSXML type library.
    Dim objXML As New MSXML2.DOMDocument
    Dim objXMLNode As MSXML2.IXMLDOMNode
   
    Set objViews = Outlook.Application.GetNamespace(Type:="MAPI") _
                   .GetDefaultFolder(FolderType:=olFolderInbox).Views
    Set objView = objViews.Add(Name:=strViewName, ViewType:=olTableView, _
                   SaveOption:=olViewSaveOptionAllFoldersOfType)
    
    'Load the schema into the MSXML parser.
    objXML.loadXML bstrXML:=objView.XML
            
    'Select the node you want to modify and assign a new value.
    Set objXMLNode = objXML.selectSingleNode(querystring:=strPropA)
    objXMLNode.nodeTypedValue = strValA
          
    'Check if optional input values exist; if so, change settings.
    If Not (IsMissing(ArgName:=strPropB) Or IsMissing(ArgName:=strValB))
    Then
        Set objXMLNode = objXML.selectSingleNode(querystring:=strPropB)
        objXMLNode.nodeTypedValue = strValB
    End If
        
    'Copy the modified XML back to the new view.
    objView.XML = objXML.XML
    'Save and apply the new view.
    objView.Save
    objView.Apply
   
End Sub 

Use the following statement to call this subroutine:

SetTableProperties strViewName:="New Table View, strPropA:="view/rowstyle", _
                       strValA:="font-size:12 pt", strPropB:="view/previewpane/visible", _
                       strValB:="0"

In the above example, the font size is increased to 12 points and the Preview pane is removed from the view.

In addition to modifying view properties, you can also add or remove entire elements from views. This is accomplished by adding or removing entire nodes from the XML representation of the view. Each node contains property settings and their associated numeric value. These values define the appearance of the view.

The following example creates a Table view and customizes it by adding a column.

Sub AddColumnToView(ByVal strName As String, ByVal strHead As String, _
                     ByVal strProp As String)
'Creates a new Table view and adds a column.

    Dim objView As View
    Dim objViews As Views
    'Set a reference to the MSXML parser.
    Dim objXMLDoc As New MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMNode
    Dim objRoot As MSXML2.IXMLDOMNode
     
    On Error GoTo AddColumnToView_Err

    Set objViews = _
        Application.GetNamespace(Type:="MAPI").GetDefaultFolder(FolderType:=olFolderInbox).Views
    Set objView = objViews.Add(Name:=strName, ViewType:=olTableView, _
                  SaveOption:=olViewSaveOptionAllFoldersOfType)
   
    'Load the XML schema into the parser.
    objXMLDoc.loadXML bstrXML:=objView.XML
    'Create a reference to the root element.
    Set objRoot = objXMLDoc.documentElement
   
    'Walk the tree to obtain a column node.
    With objRoot.insertBefore(newChild:=objXMLDoc.createElement(vType:="column"), _
        refChild:=objRoot.selectNodes(querystring:=("column").item(index:=5))
        'Add properties to the new column.
        .appendChild(newChild:=objXMLDoc.createElement(tagName:="Heading")).Text = strHead
        .appendChild(newChild:=objXMLDoc.createElement(tagName:="prop")).Text = strProp
        .appendChild(newChild:=objXMLDoc.createElement(tagName:="sort")).Text = "desc"
        .appendChild(newChild:=objXMLDoc.createElement(tagName:="width")).Text = 50
        .appendChild(newChild:=objXMLDoc.createElement(tagName:="type")).Text = "i4"
            
    End With
    'Copy the schema from the parser to the View object.
    objView.XML = objXMLDoc.XML
    'Save the schema.
    objView.Save
    'Apply the changes to the view.
    objView.Apply
 
AddColumnToView_End:
    Set objXMLDoc = Nothing
    Exit Sub

AddColumnToView_Err:
    Resume AddColumnToView_End

End Sub

Use the following subroutine to call the above subroutine:

Sub CallAdd()
    Call AddColumnToView(strName:="New Table View", strHead:="Original Sender", _
                     strProp:="urn: schemas: mailheader: original -Recipient") 
End Sub

The above example creates a new column that contains the original sender's name for all forwards in the user's Inbox. The subroutine contains hard-coded values that define the size and order of the column.

Conclusion

By using the MSXML parser you can add, remove or modify virtually any property associated with a given view. This allows you to create completely customized views that allow you to better display, organize, and share your data.

There are five different types of views available to effectively display a wide variety of data. They can be created programmatically and then customized using the MSXML parser.

While views are an effective tool for creating custom interfaces, it is their flexibility and robustness that make them a truly effective tool.