Add fields to a view
This example shows how to customize a view by using the Add(String) method of the ViewFields collection to add fields to a view.
Example
Note
The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.
You can specify which Outlook item properties are displayed in a view by adding one or more properties to the ViewFields collection for only the CardView and TableView objects. For other derived View objects such as BusinessCardView, CalendarView, IconView, and TimelineView objects, use other methods of determining which Outlook item properties are displayed within the view. For example, the fields displayed for the BusinessCardView object are determined by the electronic business card (EBC) layout associated with each displayed Outlook item.
To get the ViewFields collection for a view, use the ViewFields property of the associated View object (for example, the CardView or TableView objects). The Add method of the ViewFields collection is used to create a ViewField object that represents the Outlook item property to be displayed in the view. A ViewField object not only identifies an Outlook item property to display within the view, but it also describes how the values for that property should be displayed. You can change how individual column properties are displayed in a view by modifying the ColumnFormat property of the ViewField object.
In the following code example, ModifyMeetingRequestsView gets the TableView object that represents all the views from the user’s Inbox that are “Meeting Requests” views. The example then uses the Add method to add the “Start” and “End” fields to the ViewFields object that corresponds to the TableView object. It also changes the label for the “From” field to “Organized By”. ModifyMeetingRequestsView then saves the modified TableView object.
If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.
using Outlook = Microsoft.Office.Interop.Outlook;
private void ModifyMeetingRequestsView()
{
Outlook.TableView tableView = null;
Outlook.ViewField startField = null;
Outlook.ViewField endField = null;
Outlook.ViewField fromField = null;
try
{
tableView =
Application.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderInbox)
.Views["Meeting Requests"] as Outlook.TableView;
}
catch { }
if (tableView != null)
{
try
{
startField = tableView.ViewFields["Start"];
}
catch{}
if (startField == null)
{
startField = tableView.ViewFields.Add("Start");
}
try
{
endField = tableView.ViewFields["End"];
}
catch{}
if (endField == null)
{
endField = tableView.ViewFields.Add("End");
}
try
{
fromField = tableView.ViewFields["From"];
}
catch{}
if (fromField != null)
{
fromField.ColumnFormat.Label = "Organized By";
}
try
{
tableView.Save();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}