Views Collection

Microsoft Outlook Visual Basic

Views Collection

Views View

A collection of all View objects in the current folder.

Using the Views collection

Use the Views property of the MAPIFolder object to return the Views collection. Use Views.Item(index),where index is the object's name or position within the collection, to return a single View object. The following example returns a View object of type olTableView called Table View. Before running this example, make sure a view by the name 'Table View' exists.

Sub GetView()
'Returns a view called Table View

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

    Set olApp = Outlook.Application
    Set objName = olApp.GetNamespace("MAPI")
    Set objViews = objName.GetDefaultFolder(olFolderInbox).Views
    'Return a view called Table View
    Set objView = objViews.Item("Table View")

End Sub

		

Use the Add method of the views collection to add a new view to the collection. The following example adds a new view of type olIconView in the user's Notes folder.

Note  The Add method will fail if a view with the same name already exists.

Sub CreateView()
'Creates a new view

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

    Set olApp = Outlook.Application
    Set objName = olApp.GetNamespace("MAPI")
    Set objViews = objName.GetDefaultFolder(olFolderNotes).Views
    Set objNewView = objViews.Add(Name:="New Icon View Type", _
                     ViewType:=olIconView, SaveOption:=olViewSaveOptionThisFolderEveryone)

End Sub
		

Use the Remove method to remove a view from the collection. The following example removes the above view, "New Icon View Type", from the collection.

Sub DeleteView()
'Deletes a view from the collection

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

    Set olApp = Outlook.Application
    Set objName = olApp.GetNamespace("MAPI")
    Set objViews = objName.GetDefaultFolder(olFolderNotes).Views
    objViews.Remove ("New Icon View Type")

End Sub