Rediger

Del via


ViewFields.Add method (Outlook)

Adds the specified field to the end of the ViewFields collection for the view.

Syntax

expression.Add (PropertyName)

expression A variable that represents a ViewFields object.

Parameters

Name Required/Optional Data type Description
PropertyName Required String The name of the property to which the new object is associated. This property can be referenced by field name (displayed in the Field Chooser) or by namespace (represented by ViewField.ViewXMLSchemaName).

Return value

A ViewField object that represents the new view field.

Remarks

To programmatically add a custom field to a view, use the ViewFields.Add method. This is the recommended way to dynamically change the view over setting the XML property of the View object.

Referencing the property in PropertyName by its field name requires the localized name in the corresponding locale. For more information on referencing properties by namespace, see Referencing Properties by Namespace.

If you are adding a custom property to the ViewFields collection, the property must exist in the UserDefinedProperties collection for the View's parent folder.

If the property already exists in the ViewFields collection, Outlook will raise an error.

Certain properties cannot be added to a view using ViewFields.Add, including binary properties, computed properties, and HTML or RTF body content. For more information, see Unsupported Properties in a Table Object or Table Filter.

Example

The following code sample in Microsoft Visual Basic for Applications (VBA) adds the Subject field to the current view of the Inbox, referencing it by its field name. To avoid Outlook raising an error, it tests for the presence of the field in the ViewFields collection representing the current view of the Inbox before adding it.

Sub DemoViewFieldsAdd() 
 
 Dim oTableView As Outlook.TableView 
 
 Dim oViewFields As Outlook.ViewFields 
 
 Dim oViewField As Outlook.ViewField 
 
 Dim oInbox As Outlook.folder 
 
 
 
 Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox) 
 
 
 
 On Error GoTo Err_Handler 
 
 
 
 If oInbox.CurrentView.ViewType = olTableView Then 
 
 Set oTableView = oInbox.CurrentView 
 
 Set oViewField = oTableView.ViewFields("Subject") 
 
 If oViewField Is Nothing Then 
 
 Set oViewField = oTableView.ViewFields.Add("Subject") 
 
 End If 
 
 End If 
 
 Exit Sub 
 
 
 
Err_Handler: 
 
 MsgBox Err.Description, vbExclamation 
 
 Resume Next 
 
End Sub

The following code sample in VBA assumes the current view is a TableView, references the Message Class property by namespace and adds it to the current view of the current folder. To avoid Outlook raising an error, the code checks for the existence of this property in the view before calling ViewFields.Add.

Sub ViewFieldsAdd() 
 
 Dim oFolder As Outlook.Folder 
 
 Dim oView As Outlook.TableView 
 
 Dim oViewField As Outlook.ViewField 
 
 On Error Resume Next 
 
 Dim PR_MESSAGE_CLASS As String 
 
 PR_MESSAGE_CLASS = "http://schemas.microsoft.com/mapi/proptag/0x001a001e" 
 
 Set oFolder = Application.ActiveExplorer.CurrentFolder 
 
 If oFolder.CurrentView.ViewType = olTableView Then 
 
 Set oView = oFolder.CurrentView 
 
 'Determine if the ViewField exists in ViewFields collection 
 
 If oView.ViewFields(PR_MESSAGE_CLASS) Is Nothing Then 
 
 Set oViewField = oView.ViewFields.Add(PR_MESSAGE_CLASS) 
 
 'Persist the changes 
 
 oView.Save 
 
 End If 
 
 End If 
 
End Sub

See also

ViewFields Object

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.