共用方式為


Views.Add Method (Outlook)

Creates a new view in the Views collection.

Syntax

expression .Add(Name, ViewType, SaveOption)

expression A variable that represents a Views object.

Parameters

Name

Required/Optional

Data Type

Description

Name

Required

String

The name of the new view.

ViewType

Required

OlViewType

The type of the new view.

SaveOption

Optional

OlViewSaveOption

The save option that specifies the permissions of the new view.

  • olViewSaveOptionAllFoldersOfType The view can be accessed in all folders of this type.

  • olViewSaveOptionThisFolderEveryOne The view can be accessed by all users in this folder only.

  • olViewSaveOptionThisFolderOnlyMe The view can be accessed in this folder only by the user.

Return Value

A View object that represents the new view.

Remarks

If you add a View to a Views collection of a folder that is not the current folder, you must first save a copy of the Views collection object and then add the View to this collection object, as shown in the code sample below. This is a work-around for an existing problem which will otherwise cause a call to View.Apply for the added View to fail.

Sub CalendarView() 
 Dim calView As Outlook.View 
 Dim vws As Views 
 
 Set Application.ActiveExplorer.CurrentFolder = Application.Session.GetDefaultFolder(olFolderInbox) 
 ' Current folder is Inbox; add a View to the Calendar folder which is not the current folder 
 ' Keep a copy of the object for the Views collection for the Calendar 
 Set vws = Application.Session.GetDefaultFolder(olFolderCalendar).Views 
 ' Add the View to this Views collection object 
 Set calView = vws.Add("New Calendar", olCalendarView, olViewSaveOptionThisFolderEveryone) 
 calView.Save 
 ' This Apply call will be fine 
 calView.Apply 
End Sub

Example

The following Visual Basic for Applications (VBA) example creates a new view called New Table and stores it in a variable called objNewView.

Sub CreateView() 
 'Creates a new view 
 Dim objName As Outlook.NameSpace 
 Dim objViews As Outlook.Views 
 Dim objNewView As Outlook.View 
 
 Set objName = Application.GetNamespace("MAPI") 
 Set objViews = objName.GetDefaultFolder(olFolderInbox).Views 
 Set objNewView = objViews.Add(Name:="New Table", _ 
 ViewType:=olTableView, SaveOption:=olViewSaveOptionThisFolderEveryone) 
End Sub

See Also

Concepts

Views Object

Views Object Members