Class ViewFields (Outlook VBA)
The class ViewFields represents the collection of ViewField objects in a view. To use a ViewFields class variable it first needs to be instantiated, for example
Dim vfs as ViewFields
Set vfs = Session.CreateSharingItem.Move.Views(1).ViewFields
For Each
Here is an example of processing the ViewFields items in a collection.
Dim vfd As ViewField
For Each vfd In Session.CreateSharingItem.Move.Views(1).ViewFields
Next vfd
Add
Adds the specified field to the end of the ViewFields collection for the view.
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.
Add (PropertyName)
PropertyName: 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).
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
Class
Returns an OlObjectClass constant indicating the object's class. Here you can find possible values for
Dim oocsClass As OlObjectClass
oocsClass = Session.CreateSharingItem.Move.Views(1).ViewFields.Class
Count
Returns a Long value indicating the count of ViewField objects in the specified collection.
Dim lngCount As Long
lngCount = Session.CreateSharingItem.Move.Views(1).ViewFields.Count
Insert
Creates a new ViewField object and inserts it at the specified index within the ViewFields collection.
Insert (PropertyName, Index)
Dim strPropertyName As String: strPropertyName =
Dim lngIndex As Long: lngIndex =
Dim vfdInsert As ViewField
Set vfdInsert = Session.CreateSharingItem.Move.Views(1).ViewFields.Insert(PropertyName:=strPropertyName, Index:=lngIndex)
Arguments
The following arguments are required:
PropertyName (String) - The name of the property to which the new object is associated.
Index (Long) - Either a one-based index number at which to insert the new object, or a value used to match the ViewXMLSchemaName property value of an object in the collection where the new object is to be inserted.
Item
Returns a ViewField object from the collection.
Item (Index)
Index: The value can be a one-based integer that indexes an ViewField object in the ViewFields collection, a string that matches the ViewXMLSchemaName property value of an ViewField object in the collection, or a field name as displayed in the Field Chooser.
Dim vfd As ViewField
Set vfd = Session.CreateSharingItem.Move.Views(1).ViewFields(Index:=1)
Remove
Removes an object from the collection.
Remove (Index)
Index: Either the index number of the object, or a value used to match the ViewXMLSchemaName property value of an object in the collection.
Dim lngIndex As Long: lngIndex =
Session.CreateSharingItem.Move.Views(1).ViewFields.Remove Index:=lngIndex
Session
Returns the NameSpace object for the current session.
The Session property and the GetNamespace method can be used interchangeably to obtain the NameSpace object for the current session. Both members serve the same purpose. For example, the following statements perform the same function: