Developing Custom Forms Using Microsoft Outlook 2002 (Part 1 of 2)

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

 

Bill Jacob and Frank C. Rice
Microsoft Corporation

January 2003

Applies to:
     Microsoft® Outlook® 2002

Summary:   Learn about the core concepts related to developing custom forms using Microsoft Outlook 2002. This article is the first of two articles that cover some of these concepts. The two lessons in this article will help you understand what happens behind the scenes and provide techniques that can help you take advantage of custom forms in your Outlook solutions. (40 printed pages)

Contents

Lesson 1: Working with Items and Forms
  Items Versus Forms
  Considerations When Selecting a Form
  Saving, Publishing, and Distributing Forms
  Message Classes
  One-off Forms
  How Outlook Locates a Form
Lesson 2: Fields
  How Outlook Fields are Structured
  User-Defined Field Behavior
  Using the Field Chooser
  Validating Field Data
  Setting Initial Values on a Field
  Having Fields Automatically Update

Lesson 1: Working with Items and Forms

Overview

This lesson covers some of the core concepts related to developing custom forms using Microsoft® Outlook® 2002. In particular, it focuses on the relationship between an item and the form that is used to display the item. The information in this lesson will help you understand what is happening behind the scenes and show how these concepts can impact a solution you are designing.

What You Will Learn

After completing this lesson, you will be able to:

  • Understand the subtle difference between the terms item and form.
  • Explain where a custom form should be published under various circumstances.
  • Understand what the Message Class field is and what role it plays in custom forms and Outlook solutions.
  • Explain where Outlook looks for custom forms when launching items.
  • Describe the purpose of the forms cache and the key limitations of the cache.
  • Understand what one-off forms are, how they can be created, and what the typical side effects are.

Items Versus Forms

The terms item and form can sometimes be used interchangeably, but they typically are used in different contexts.

An item is an actual item in a folder, whether it is a task, contact, or mail message. An item is basically like a record in a database. And also like a database, each item has a collection of fields. Each type of Outlook item has a standard set of fields for that type of item. These fields are called standard fields. In addition to Outlook's standard fields, you can create user-defined or custom fields, for each item. In its simplest terms, an item is a container for information. The term item implies a physical item in a folder that stores data within its fields.

A form is basically the user interface or front end to an item in the folder. It is typically made up of various fields that are used to display the underlying data stored within the item.

You can use any form to view the contents of any item, but there is a clear distinction between a form and an item. All data is stored in an item, and the form is just the viewing mechanism, or user interface. This is an important distinction that will play an important role in topics to follow.

The distinction between an item and a form is a common area of misunderstanding. This is especially true since to create a custom form, you start off by opening a new item. However, the new item should be viewed only as a mechanism for creating a new form.

When using other messaging technologies such as MAPI or the Collaboration Data Objects (CDO) object library, items are referred to as messages. This is not the same as a mail message. In this context, the term message means any item in a folder, regardless of its Outlook type. These terms often can cause miscommunication when speaking with other people. It is imperative to be very clear on exactly what is being talked about in relation to messages, items, and forms.

Note  You can find more information on the CDO object library by reading the article Introduction to Collaboration Data Objects for Microsoft Exchange 2000. You can find more information about MAPI in the article Introduction to MAPI.

Considerations When Selecting a Form

Some standard Outlook forms may be more useful for particular applications than others. There are generally many things to take into account when designing a custom forms solution, but the following are the more important considerations to take into account.

Routing Versus Folder-Based Solutions

Mail messages are sent (or routed) to individuals. All other items are primarily designed to be stored in a folder, including the Post form (discussed shortly), which is very much like a mail message in terms of the types of standard fields it contains. So if your form requirements are that the form must be sent to other people, the mail message is the form that should be customized.

Standard Fields on a Form

Each type of Outlook item has a different set of standard fields associated with it. To see a complete list of fields for an item, click All <form type> fields in the Field Chooser dialog box. (Outlook displays the Field Chooser dialog box whenever you open a form in design mode. You can open a form in design mode by pointing to Forms on the Tools menu and then clicking Design a Form. Open the Contact form.) For example, to see all the fields that are available in a mail message, click All Mail Fields (see Figure 1). After choosing the type of form that best suits your needs, you can then create custom fields by clicking New in the Field Chooser dialog box.

Aa155722.odc_olcustfrm101(en-us,office.10).gif

Figure 1. Field Chooser dialog box

Customizable Pages on the Form

Most Outlook forms have more than one page (or tab) on the form. For example, the Contact form has General, Details, Activities, Certificates, and All Fields pages. With the exception of the Mail and Post forms, and the General page of the Contact form, all of Outlook's standard form pages are not customizable. To change many of the existing Outlook form pages, you need to hide the existing page on the form, create a new page, and add fields or controls to that page.

Table 1. Items used to customize pages

Use this item To Modify default pages?
Mail message Send a form to other people with an associated Reply form so they can send information back. Yes
Post Send a form to a folder. Yes
Contact Record information about a person or organization. Only the General page.
Distribution list Create distribution lists for your contacts. No
Task or Task Request Record information about an action that is due at a particular time. No
Appointment or Meeting Request Record scheduling information about people, dates, and time. No
Note Keep simple notes in a folder. Not customizable at all, other than changing settings on the control menu.
Office Form Integrate Office documents on a form. No

Try This: Create a Custom Outlook Form

In the following steps, we will start to build a custom contact form by adding fields and extra pages. First, we will create a folder called Vet Contacts. Next, we will create and publish the custom contact form (called the Customer form) into the Vet Contacts folder and change the folder properties so that new items in this folder will be based on our Customer form. This is the most basic scenario when creating a custom form solution.

Note  If the Forms menu item is missing from the Tools menu, you will not be able to display the design environment of the form. This occurs if you are using Microsoft Word as your e-mail editor. Temporarily turn off Word as your e-mail editor, and then reopen the form. To turn off Word as you e-mail editor, in the main Outlook window, click Options on the Tools menu, and then click the Mail Format tab. Clear the Use Microsoft Word to edit e-mail messages check box.

Create the Vet Contacts folder

  1. On the File menu, point to New, and then click Folder.
  2. In the Name box, type Vet Contacts as the name for the folder.
  3. In the Folder contains box, click Contact Items.
  4. In the Select where to place the folder list, click Mailbox - <username>, and then click OK to place the new folder.

Build the custom contact form

  1. Switch to your Vet Contacts folder and open a new Contact item by pointing to New on the File menu and clicking Contact. This will be the basis of our custom form.

  2. With the Contact form open, on the Tools menu, click Forms and then click Design This Form. This switches the item into design mode. Note that you can modify the General page on the form.

  3. Click on the (P.2) page, which is by default a hidden page. All page names enclosed in parenthesis are hidden pages.

  4. On the item's Form menu, click Display This Page. Note that (P.2) changes to P.2 to indicate that the form is no longer hidden.

  5. On the item's Form menu, click Rename Page and then type Pet Info as the new name for this page.

  6. Set the Field Chooser dialog box to show All Contact Fields.

  7. Drag the Categories and Categories… fields from the Field Chooser dialog box onto the Pet Info page. Note that the Categories… field will add a button to your form that will display the Categories dialog box when clicked.

  8. Drag the Full Name field from the Field Chooser dialog box.

  9. Drag the Birthday field from the Field Chooser dialog box. If desired, rearrange the fields to resemble those in Figure 2.

    Aa155722.odc_olcustfrm102(en-us,office.10).gif

    Figure 2. Custom form in design mode

  10. On the Form menu, click Run This Form. This will create a new item based on the form and show you the form as it will appear when being used.

  11. Close and do not save changes to this form that is in run mode, returning to the form in design mode.

  12. On the Tools menu, click Forms and then click Publish Form As... . The Publish In setting should default to the Vet Contacts folder since this is the active folder.

  13. Type Customer in the Display Name box and click Publish.

  14. On the File menu, click Close. When prompted to save changes, click No. Note that saving changes would mean that a new item would be created in the folder, which is typically not what you want to do when designing a form.

  15. On the Actions menu, notice a menu item at the bottom called New Customer. This item is available because the form was published in the folder. Click New Customer to open a new item (form) based on the custom Customer form.

  16. Close this new item and do not save any changes, if prompted.

  17. Click on the first toolbar button (New button) to create a new contact item. Notice that the new item uses the default Outlook contact form and not the custom Customers form. Close and do not save the new item.

  18. On the File menu, point to Folder, and then click Properties for "Vet Contacts." Note the setting for the option labeled When posting to this folder, use. It should be set to Contact. Change this setting to Customer and then click OK.

  19. Once again, click on the first toolbar button to create a new contact item. This time you should see the custom Customer form being used.

  20. Close and do not save changes to the form.

Congratulations! You have created a custom Outlook form solution!

Important things to remember from this exercise

  • You cannot change many of Outlook's default pages.
  • After publishing the form, you do not have to save the item. This is related to understanding the difference between items and forms.
  • Forms should typically be published into the folder if the form is to be used solely within that folder.
  • Any form published in a folder will appear under the corresponding Actions menu for that folder.
  • To use the first toolbar button to access a custom form, you need to set the custom form as the default form for the folder.

For now, we will continue to use these same steps when creating custom forms. There are many variations, options, and additional considerations when creating custom forms, but this is the simplest scenario involving a single-user solution. We will first take a look at working with the form itself, and further cover publishing options.

Loss of Standard Outlook Form Functionality

When customizing Outlook forms, such as in the previous procedure, there are some areas where a customized form may not behave like a standard Outlook form. For example, click the Contacts folder and open a standard Outlook form (click New Contact on the Action menu). Click the Details tab, and then click the down arrow by the Birthday box. Notice that a calendar control is displayed. Now, click the Vets Contacts folder, open the Customer form (click New Customer on the Action menu), and then click the Pet Info tab. You should see the three custom fields that you added to the Pet Info page. Notice the following:

  • The Birthday field no longer has the calendar control attached to it and you have to manually type in the date. The field is now a regular text box. This is mainly because the calendar control that is available on the default Outlook form pages is not directly available as a separate control, although you can use various custom ActiveX® controls to simulate the same look and feel.
  • If you type John Smith in the Full Name field, press ENTER, click the All Fields page of the form, and then change the Select from drop down to Name fields, then Outlook automatically populates the other name fields for the item. This is because the underlying field data is still processed as usual. Most limitations are related to controls or user interface limitations.
  • Existing controls may shift slightly when you enter design mode. This is particularly noticeable on the first page of a Post form. If you customize an existing form page, such as by deleting the message control from the first page of the Post form, then the controls will stay shifted when the form is switched to run mode.

You should not assume that a custom form would behave in the exact same manner as a standard Outlook form, even if you are using the same features. As you are planning a solution, test the planned features as you go to make sure they will perform as you expect. Note that in many cases, you can use Microsoft Visual Basic® Scripting Edition (VBScript) to replicate any functionality that may be lost.

Saving, Publishing, and Distributing Forms

Saving a Form as a Template (OFT) File

It is a good idea to save a backup copy of any forms you create. Outlook allows you to save a form as a separate file with an .oft extension. This also makes it convenient to transfer a form to someone else. To do this:

  1. Open an item that is based on the custom form you wish to save. You can simply click the first toolbar button if the form is the default form from the folder, or select the form from the bottom of the Actions menu.
  2. From the File menu, click Save As.
  3. If it is not already, change the Save as Type setting to Outlook Template (*.oft).
  4. Specify a name for the template file and click OK.

Publishing Custom Forms

In most scenarios, forms should be published and not saved. Publishing a form means that you are storing the form somewhere so that it can be used at a later time to view items. Publishing means that you are saving it to a MAPI-based store, instead of a file in the file system.

To publish a form in a forms library:

  1. With the custom form in design mode, on the Tools menu, point to Forms, and then click Publish Form As.

  2. In the Look In dropdown, select the location you wish to publish the form to.

  3. Set the Display name to the name of the form and click Publish.

    Click here for larger image

    Figure 3. The Publish Form As dialog box (click picture for larger image)

There are generally three places to which you can publish a form:

  • A folder (or Folder Forms Library).  In the vast majority of folder-based solutions, you should publish the form to the folder so that is is available whenever someone is using the folder. If you publish the form in a public folder, the form is then available to everyone using the folder, or if you publish it to one of your personal folders, it is available whenever you are using that folder. There is also an offline library which allows users in remote locations to use the forms as if they were connected directly to the server. One advantage to publishing to a folder is that the forms are then available at the bottom of the Actions menu while you are in the folder.
  • Organizational Forms Library.  Forms saved in this library are accessible to everyone in the organization and are stored on a Microsoft Exchange Server. The server administrator must give you permission to save to the Organizational Forms Library. To access these forms, use the Choose Form dialog box that is available by clicking New from the File menu. Use this library when you want to make the form available to everyone in your organization, such as a form to report vacation time. This library is often used for mail message forms, and is often typically used when the same form is used across multiple users or public folders. In this scenario, there is only one form that can be centrally administered.
  • Personal Forms Library.  Forms saved in this library are accessible to only you and are stored (hidden) in the root of your Exchange Server mailbox or in a local PST file, whichever is your default delivery location. Like forms stored in the Organizational Forms Library, these forms can be launched using the Choose Form command. Use this library when you have created a form for your own personal use, such as a mail message form that ispre-addressed to some recipients. However, many people publish forms such as this in the Inbox, since they are easily available at the bottom of the Actions menu.

There are other places you can save a form to, but these are mostly "theoretical locations" and not typically used in the real world. However, these locations are sometimes used as a means to distribute forms to a user who is offline.

  • Save the form as an OFT file.  This allows you to send the form to someone else to work on, or you can use the OFT as a backup. To save a form as an OFT file, on the File menu, click Save As. In the File name box, type a name for the form. In the Save as type box, click Outlook Template.
  • Save the form as an item in the current folder.  This is generally not recommended, as it is relatively easy to wind up deleting the form. It also tends to blur the distinction between forms and items, and it is easy to wind up with multiple versions of the form by opening different items. To save a form in the current folder, on the File menu, click Save.

Deciding Where to Publish the Form

The types of forms solutions you can create using Microsoft Outlook can vary greatly, especially given the various types of forms you can use in Outlook. It is often difficult to figure out the best place to publish a form, and there are usually a lot of things to take into account when making the decision, for example:

  • How many people will need access to it?
  • Will it be updated often, making it important to only have one copy of the form deployed centrally?
  • How will the form be launched?
  • Does it need to be available offline?

When creating a folder-based solution, where the primary focus of the form is to display the items in a single folder, you should typically publish the form to the folder itself. This way the folder is a self-contained entity and the form is readily available under the Actions menu. However, if you are going to be using the form in more than one personal folder, and each folder will always use the same version of the form, you should consider publishing the form into the Personal Forms Library. This way there is only one copy of the published form and it is generally easier to update if you need to make changes. On the other hand, the down side to this approach is that the form is less accessible since it will not be available under the Actions menu. However, if it is the default form for the folder, as is typically the case, this should not make too much of a difference since you will probably use the first toolbar button to launch new items. The same guidelines hold true for forms in public folders, except you would publish the form to the Organizational Forms Library instead of the Personal Forms Library.

If the form is based on a mail message and is to be used just by you and maybe a couple of people, then it would be best to publish the form to everyone's Personal Forms Library. However, if you are going to be creating new items based on this form, you may want to publish it to your Inbox so that it shows up under the Actions menu of the Inbox. If the form is going to be used by many people within an organization, then it should be published in the Organizational Forms Library so that there is only one copy of the form to maintain and everyone has access to it. Note, however, that this library is located on an Exchange Server which, depending on network and server traffic, can make the form more difficult to launch.

Message Classes

Understanding the concept of message classes is critical to understanding how to use forms with Outlook. Since message classes are relatively undiscoverable to a typical Outlook user, they can be the root cause of many usability issues with custom Outlook forms.

Each Outlook item has a standard field called a message class. This field identifies which form should be used to view a particular item. So every time an item is opened, Outlook looks at the message class field and uses that information to decide which form should be used to view the item. Each type of Outlook item has a standard, unique message class, some of which are detailed in the following table.

Table 2. Message classes for Outlook items

Item Message Class
Mail Message IPM.Note
Post IPM.Post
Contact IPM.Contact
Distribution List IPM.DistList
Task IPM.Task
Task Request IPM.Task (prior to sending)

IPM.TaskRequest (in the recipient's Inbox)

Appointment IPM.Appointment
Meeting Request IPM.Appointment (prior to sending)

IPM.Schedule.Meeting.Request (in the recipient's Inbox)

Note IPM.StickyNote
Journal Entry IPM.Activity

If you customize one of the default Outlook forms and then publish it to a folder or forms library, the name given at that time is appended to the standard message class for that item type to create a unique message class for that form. For example, if you customize a contact form, publish it, and name it MyForm, the message class for the published form will be IPM.Contact.MyForm. Any new items that use this custom form will have their message class field set to IPM.Contact.MyForm (see Figure 4).

Click here for larger image

Figure 4. Message class for My Form (click picture for larger image)

If MyForm is published to the Contacts folder, then under the Actions menu, you will have a New MyForm option. This will create a new contact item and its message class will be set to IPM.Contact.MyForm. However, if you click on the New Contact toolbar button, you will get a new default Contact form. As we saw in the previous Try This, this is because the default form for the contact folder is still set to Contact. To change this:

  1. Display the Properties for the Contacts folder (either point to Folder on the File menu, and then click Properties for "Contacts" or right-click the Contacts folder and click Properties).

  2. In the When posting to this folder, use setting, select your custom form.

  3. Click OK.

    Note  To return the default back to the Contact form, set the When posting to this folder, use setting back to Contact.

It is important to note that importing or exporting items through the Import/Export Wizard does not allow you to specify a message class for the new Outlook items. All of the new items are assigned the standard message class depending on the type of item being imported.

Note  For a complete list of messages classes and how each one is used by Outlook, see the Item types and message classes topic in the Outlook Help file.

When working with custom forms, is is a good practice for you to always keep track of the message class fields. It is recommended that you add the message class field to the table view for your custom folders so that you can be sure that each item is actually using the proper form. To do this, click your custom folder and then:

  1. Point to Current View on the View menu and then click Customize Current View.
  2. Click Fields on the View Summary dialog box.
  3. In the Select available fields from drop-down list, click the appropriate All <folder type> fields selection for the type of custom folder you created. For example, click All Contacts fields for a contact-type custom folder.
  4. In the Available fields list, click Message Class, and then click the Add -> button.
  5. In the Show these fields in this order list, click Message Class, and then click either Move Up or Move Down to position the field where desired.
  6. Click OK.

One-off Forms

Many forms-related support issues revolve around the concept of one-off forms. When creating custom form solutions with Outlook, it is important to be able to recognize how Outlook is using your custom form in relation to the items in a folder.

A Typical Custom Forms Solution

If you want to create a custom contact form to replace the default Outlook contact form, you would typically follow these steps:

  1. Start with a new, default contact form to use as the basis for your custom form.
  2. Customize the form to suit your needs.
  3. Publish the form to the Contacts folder.
  4. Set the form as the default form for the Contacts folder by changing the folder properties.

As we saw in the previous section, each Outlook item has a standard message class field to keep track of which form should be used to display the item. The default message class for a contact is IPM.Contact and when the form is published, the message class is changed to reflect the name of the published form. For example, if you created a form called MyForm, when it was published, the form would have a message class of IPM.Contact.MyForm. When you create a new item based on the published form, the message class of the item is set to IPM.Contact.MyForm.

In the typical scenario above, information about the form (the form definition) is not saved with each item. The form is basically "linked" using the message class information. The size of a typical item is usually 300 or 500 bytes. However, it is possible to have the form definition saved with an item. Items that have a custom form definition stored within them are referred to as being "a one-off" or "one-offed".

When a form is one-offed, it uses the form definition that is stored with the item instead of the published form. If a form definition is saved with an item, the item's message class is changed back to the default message class for that form type, such as IPM.Contact.

Standard Ways Forms Become One-offs

The ability to have the form definition stored within an item is actually one of Outlook's features. You can specify that a form is a one-off by selecting the Send form definition with item check box on the form's Properties page with the form in design mode (see Figure 5).

Aa155722.odc_olcustfrm105(en-us,office.10).gif

Figure 5. Send form definition with item check box

You would typically do this with a mail message form when the form is not registered in a forms library that the recipient has access to. Selecting this option ensures that the person will see the custom form when the item is received and then opened. In these types of scenarios, one-off forms work as expected and generally do not create any issues. However, various scenarios can cause an item that does not contain a form definition to unexpectedly have one stored within it. These scenarios often can cause undesirable side effects, especially because once a form definition is stored within an item, there is no direct way to backtrack and have it fully removed.

Items One-offed Unexpectedly

Assume you have a typical custom forms scenario where the form is published in a folder. You also have ten items already created that are using your published custom form. If you open one of the items and the form definition is changed for that one particular item, Outlook will automatically one-off the item (create a one-off form) and save the form definition with the item. The message class will change back to the default message class for that type of item, and the size of the item will noticeably increase.

The following are known scenarios where items can be unexpectedly one-offed:

  • You modified the form of an existing item instead of creating a new item and modifying its form. If you want to change the form for all of the items in the folder, instead of opening an existing item, follow these steps:
    1. Open a new item based on your custom form.
    2. Make form design changes to that item.
    3. Using that item, re-publish the form with the same name.
    4. Close and do not save changes to the item.

All of the items in the folder will now use the updated custom form the next time the items are opened because the message class still refers to the same published form. The first time an item is opened after the form is changed, Outlook will briefly display a message indicating that the new form is being installed (copied to the forms cache).

  • You used VBScript code in the custom form and used some methods or commands that are causing the item's form definition to change. The following methods most commonly cause this behavior:
    • UserProperties.Add to programmatically add a user-defined field to an item.
    • Methods of the FormDescription object.
    • Methods related to the properties of controls, usually for enabling or disabling controls or making them hidden or shown.
    • Methods related to the Action object.
  • You created a field for use in the folder but the field did not actually exist in the item when the form was published. In this case, if you create a new item based on the custom form and use the Outlook in-cell editing feature to enter a value into an item in a table view, Outlook automatically adds the user-defined field to the item dynamically. This changes the form definition and therefore the item is one-offed.

Typical Symptoms of One-offed Forms

Although solutions and situations vary greatly, there are some signs to be aware of that can indicate that forms are becoming one-offed:

  • A macro virus warning is unexpectedly displayed when an item is opened. One-off forms are a common cause of the "Enable/Disable" macro security warning Outlook displays if a form contains VBScript code. Because the code is not in a published or trusted form but actually in the item itself, Outlook will always display the warning. Ideally you should try to avoid the scenario that is causing the form to be one-offed in the first place.
  • The size of an item increases unexpectedly.
  • The icon for an item changes unexpectedly, typically indicating a change in the message class field.

Identifying a One-off Item

If an item is unexpectedly one-offed, and then you notice that the Send form definition with item check box is not selected, that does not necessarily mean the form definition is not saved with the item.

An item is a one-off if its message class information is set to the default message class for that type of item, but when the item is opened, the form used to display the item is a custom form.

When developing a folder-based solution, you may want to display the message class and size information in a table view so you can be sure that the items in the folder are behaving as you expect. Also, a change in the icon for an item oftentimes indicates a status change although you should restart Outlook to be sure the proper icon is being displayed.

Try This: Creating a One-off Form

The following steps illustrate how the UserProperties.Add method can one-off an item:

  1. In the Inbox folder, click Tools, point to Forms, and then click Design a Form. The Design Form dialog box is displayed.

  2. Click Inbox in the Look In box, click Message, and then click Open.

  3. Using the Control Toolbox (on the Form menu), add a CommandButton control to the P.2 page.

  4. On the Form menu, click View Code.

  5. Add the following VBScript code into the VBScript Editor and then close the editor:

    Sub CommandButton1_click()
       ' Changes the form definition by adding a field.
       Item.UserProperties.Add "MyField", 1
       ' Send the item
       Item.Send
    End Sub
    
  6. On the Tools menu, point to Forms, click Publish Form As, and then publish the form in the Inbox folder as OneOff.

  7. When prompted to enable the Save form definition with item option, click No. Close and do not save changes to the form.

  8. On the Actions menu, click New OneOff to open an item based on the form.

  9. Address it to yourself.

  10. Use the command button on the P.2 page to send the message. Depending on your security settings, you may receive a warning that another program is trying to send an e-mail message. Click Yes to send the message.

  11. When you receive the message, open it to read it.

Note Depending on your security settings, the macro warning appears. Also, the size of the item is relatively large (~6K).

Working Around Unwanted One-offs

If your solution design dictates that an item will become a one-off, you can work around the side effects of a one-off item by resetting the message class and then saving the item in code. As an example, use the following code instead of the code given above in the Try This section and, depending on your security settings, the item should not enable the macro warning message.

Sub CommandButton1_click()
  ' Save the MessageClass property
  sSaveMessageClass = Item.MessageClass
  ' This changes the form definition
  Set MyProp = Item.Userproperties.Add("MyField", 1)
  ' Restore the MessageClass property
  Item.MessageClass = sSaveMessageClass
  ' Save the changes to the form
  Item.Save
  ' Send the item
  Item.Send
End Sub

A much more flexible approach would be to use to the PropertyChange event to trap when the MessageClass property changes, and then reset the message class accordingly. But since we have not covered the Outlook object model or VBScript in any detail, the simplistic approach taken above illustrates that this approach works.

Note   If you programmatically change the message class of an item, the size of the item is not reduced. There is no way to truly remove the unwanted form definition from an item other than creating a new item without the form definition and copying the data from the old item to the new one.

How Outlook Locates a Form

When an item is opened from a folder, Outlook checks the message class field to see what form should be used to display the item. It then conducts a search for the required form, looking in the following locations:

  • Memory.  If the form has been accessed some time after Outlook was started and is still available in memory, Outlook will use it. If an item is open that is based on your custom form and you re-publish the form and then open a new item, Outlook will not use the updated form because the form is already being used for the previous item.
  • Forms Cache.  If the form is not in memory, Outlook will look in the forms cache on your computer to see if you have used the form before. If the form is located in the cache, Outlook will load the form from your hard drive.
  • Current Folder.  If the form is not in the cache, Outlook will look in the current folder for the form. Other folders, including parent folders, will not be searched. Forms that are published to a folder are stored as hidden items and cannot be seen in any of the Outlook views.
  • Personal Forms Library.  If the form is not found in a folder, Outlook checks the Personal Forms Library. The Personal Forms Library is stored in the top-level folder of the default store, which is the set of folders that contains your default Inbox (the Inbox that receives incoming mail).
  • Organizational Forms Library.  If the form is not available in the Personal Forms Library, then Outlook will check the Organizational Forms Library on the Microsoft Exchange Server.
  • Web Forms.  If they are configured in Options from the Tools menu.

If a form cannot be found, then the item is displayed using the standard form for that type of item (standard mail message for a custom mail message, standard contact form for a custom contact form, and other forms).

If the form is found in the forms cache, the last modified time is compared to the last modified time of the form in the original location. If the original location contains an updated form, Outlook will download the updated version and update the forms cache.

Purpose of the Forms Cache

To increase performance when opening forms, Outlook stores a copy of the form in a cache so it does not have to retrieve the form every time an item is opened. When forms are stored in a Microsoft Exchange public folder or in the Organizational Forms Library, using a cached version of the form increases performance considerably on both the local computer and the server.

Where the Form Cache is Located

Unlike previous versions of Outlook, the forms cache is no longer located in the C:\Windows\Forms folder. The new locations in Outlook 2000 and later versions support roaming users. In Microsoft Windows® XP, the forms cache is located at:

C:\Documents and Settings\<username>\Local Settings\Application Data\Microsoft\Forms

If you are using Microsoft Windows NT®, the forms cache is located at:

C:\Windows\Profiles\<username>\LocalSettings\Application Data\Microsoft\Forms

How the Forms Cache is Structured

When a form is activated for the first time, the form definition file is copied from its forms library to the Forms folder. The forms cache keeps a temporary copy of the form definition in a subfolder whose name roughly matches the name of the form.

The form table, Frmcache.dat, also located in the Forms folder, is used to locate a form and to prevent multiple instances of the same form from being loaded in the cache. When a form is activated, Outlook checks to see if a form with the same message class is already in the cache. If not, it copies the form definition to the cache. In addition, if a change has been made to a form, Outlook copies the new form definition to the cache.

Outlook looks for forms in the following order. When a match is found, Outlook opens the form and does not search further:

  • Standard Outlook forms, such as Note, Post, and Contact, in the Application Forms Library.
  • Forms already cached in the form cache.
  • Forms in the Personal Forms Library.
  • Forms published in the folder that iscurrently selected.
  • Forms in the Organizational Forms Library.

Because Outlook caches forms, you should try to avoid having more than one form with the same name or publishing the same form to more than one forms library. Forms used in a folder-based solution should be published only in the folder. If you are developing a solution based on mail message forms, you can temporarily publish the forms in your Personal Forms Library. Once the form is finalized, you should publish the form to the Organizational Forms Library on the Microsoft Exchange Server and delete the form from your Personal Forms Library after making a backup of the form. If, for some reason, you need to publish a form in more than one location, you should be sure to keep all forms libraries up-to-date with the current version of the form.

Outlook sets a 2048 KB limit on the total size of the forms cache by default. To change this setting, follow these steps:

  1. On the Tools menu, click Options.
  2. Click the Other tab, and click Advanced Options.
  3. Click Custom Forms, and under Maximum space on hard disk, enter a number greater than 2048, and then click OK.

Once the maximum disk space for the forms cache is reached, the oldest unused forms are purged from the cache.

If the form is found in the forms cache, the last modified time is compared to the last modified time of the form in the original location. If the original location contains an updated form, Outlook will download the updated version and update the forms cache. We will discuss the forms cache later in this article.

Cannnot Change the Forms Cache Location or Contents

Outlook 2002 was designed to support roaming users and therefore the forms cache location can change depending on which user is currently logged on to a computer. There are no user-configurable settings to indicate where the forms cache is located. Therefore, it is not possible to directly change the location of the forms cache to a location other than the default location.

The format of the Frmcache.dat file is undocumented and therefore direct modification of the file is unsupported. Microsoft cannot support direct modifications to any of the contents of the Forms folder unless such changes are documented by Microsoft, or recommended by Microsoft Product Support Services for troubleshooting purposes only. Before making any modifications to the contents of the Forms folder, you should first make a backup.

Lesson 2: Fields

Overview

Fields play an integral role in every Outlook solution since they store the data that the solution depends on. Unlike typical databases, Outlook presents some challenges for the solution developer since many of the concepts revolving around Outlook and Messaging Application Programming Interface (MAPI) fields are not consistent with other types of applications. In this lesson, we will look at how fields are implemented in Outlook and how you can get the most out of them for your solution.

What You Will Learn

After completing this lesson, you will be able to:

  • Explain the difference between standard and user-defined fields.
  • Understand that user-defined fields can exist in various locations, and that inconsistencies can adversely affect your solution.
  • Better understand how to get the most out of the Field Chooser dialog box.
  • Explain what each of the Microsoft Outlook field types are used for.

How Outlook Fields are Structured

Working with fields is an important aspect of customizing Outlook forms. In this section, we will take a close look at all aspects of fields to provide a good foundation for customizing forms.

Outlook fields are stored in a MAPI-based database, or store, which can be a folder on an Exchange Server or a local personal folders file (*.pst) file. An Outlook item is composed of a group of fields, and directly parallels a record in a traditional database.

In a typical flat-file database system (like Microsoft Excel) or a relational database system (like Microsoft Access), fields are usually defined first and then each record in the database (or database table) has the same set of fields. However, MAPI-based stores are called "semi-structured" since they allow an application to store items in a folder that have different sets of fields. In Outlook, an item's structure is based solely on conventions that are part of Outlook's design.

Each type of Outlook item contains a specific set of standard fields. For example, a contact has one set of standard fields, while a mail message has another set of standard fields. Some standard fields are available on all types of items, while other standard fields may be available on only one type of item or a subset of items.

In addition to the standard fields that are available on items, Outlook allows you to add any number of user-defined, or custom, fields to an item.

Standard Fields

Each Outlook item has a set of standard fields. The contact items, for example, have standard fields to store information specific to contacts, such as Full Name, Email1Address, Birthday, and others. Some of the fields, such as Subject, are available in all types of forms, and some of the fields are only available to one form. But from a solution perspective, working with Outlook's standard fields is intuitive since these fields will always exist in every item based on the standard form. For example, all contact items will at least contain all of the standard contact fields.

The Message (or Notes) Field

The Message or Notes field is a standard Outlook field on each type of item. On a mail message or request form it is referred to as the Message field, but on other forms, such as the Contact or Task form, is is called a Notes field. The functionality is the same.

With the Message field, you can:

  • Insert inline file attachments or shortcuts. Mail message forms must use an RTF format in order for attachments to be viewable within the text stream.
  • Insert item attachments or shortcuts. By doing so, you can insert messages from other users or other items in the Message field.
  • Insert a shortcut to a Web page.
  • Insert linked or embedded objects. These objects can be files such as voice mails, Word documents, Excel workbooks, and Microsoft PowerPoint® presentations.

Each Outlook item contains only one standard field to store the data of a Message or Notes field, and it is not possible to create a user-defined field of this type, so each item is limited to only one rich text field.

Note  When programmatically referring to the Message field, is is referred to as the Body property.

Recipient Fields

Recipient fields are basically standard fields that are designed to resolve e-mail addresses. These includes To, From, CC, and BCC, as well as the e-mail, phone, and Web page fields on the contact form. These fields use a custom Outlook control that is specifically designed to handle the resolution of e-mail addresses, parsing of e-mail names that are separated by semicolons, and handling such operations as copy and paste within or between the fields.

When working with these fields on a custom form page, you should drag the standard field from the Field Chooser dialog box onto the form page. Outlook will automatically create a recipient control and set everything up for you.

Note  If you view the data type of a recipient field, Outlook lists it as Recipient or Internal Data Type.

Contact "Selector" Fields

Outlook makes available additional fields to support the drop-down buttons on the first page of a Contact form. These fields are:

Table 3. Contact selector fields

Selector Fields Selected Fields
Address Selector Address Selected
E-mail Selector E-mail Selected
Phone 1 Selector Phone 1 Selected
Phone 2 Selector Phone 2 Selected
Phone 3 Selector Phone 3 Selected
Phone 4 Selector Phone 4 Selected
Phone 5 Selector Phone 5 Selected
Phone 6 Selector Phone 6 Selected
Phone 7 Selector Phone 7 Selected
Phone 8 Selector Phone 8 Selected

To use these fields on a Contact form page, switch into design mode and then use the Field Chooser dialog box to drag the Selector field you wish to use onto the page. This will automatically create a label and button for the drop down list. You can then drag the corresponding Selected field onto the form.

When you drag the Selector field onto the form, Outlook generates a special button that will display a drop-down list when clicked. However, the button that is created will differ in two respects:

  • The Selector button will be smaller than Outlook standard buttons, but you can resize the button.
  • The black, triangle-shaped down arrow on the standard buttons is not automatically created for a new Selector button, but you can substitute another graphic. The down arrow bitmap is not available from Microsoft. However, you can create a down arrow bitmap in the standard software drawing packages such as Microsoft Paint.

To select a graphic for the button, follow these steps:

  1. Right-click the button and click Advanced Properties.
  2. Click the Picture property and click the ellipses (...) button at the top of the Properties window to select another graphic file.

Note  These Selector drop down buttons cannot be recreated by using the Control Toolbox to drag controls onto the form and then binding the controls to the button. The only way to create functional drop down buttons is to drag the Selector fields from the Field Chooser dialog box.

User-Defined Field Behavior

Unlike most other database related software, Outlook can store different sets of user-defined fields in different items in a folder. While this means that items in a folder are very flexible containers for storing data, it also means that developers need to be aware of how this flexibility may impact an Outlook-based solution.

In a database program such as Access, fields are defined at the "table level," meaning that once you add a field to a table of data, the field will exist in all records in the table. Outlook items are the functional equivalent to records in a database, and Outlook folders are conceptually equivalent to a table or flat file database. However, this database analogy breaks down when you consider how Outlook and Microsoft Exchange implement the concept of fields, or properties.

In Exchange, items in a folder can have any set of fields. It is up to the application to determine the "field schema" for items in a folder. Outlook has separate field schemas for each of the different predefined items such as Contacts, Appointments, Tasks, and such. Each of these items has a set of standard fields specific to that type of item. Note that some fields exist in more than one type of item, such as the Start field in Appointments and Journal entries, and some fields exist in all items, like Mileage and Billing Information.

In addition to the standard fields that Outlook makes available for each type of item, Outlook allows you to create user-defined, or custom, fields to store additional information with an item. While these user-defined fields serve the same purpose as the standard fields, you typically use them in a different manner. The most important difference between standard fields and user-defined fields is that standard fields are always available when working with a particular type of item, but this is not necessarily true when working with user-defined fields. User-defined fields can exist in:

  • Folders, so that you can use fields in folder-related scenarios such as using the Field Chooser dialog box to add custom fields to a table view.
  • Items, so that data can be stored in the fields.
  • Published forms, so that new items you create based on the custom form will inherit the user-defined fields.

In most cases, Outlook will replicate user-defined fields between items and folders dynamically, and this is usually transparent to most users of Outlook. However, if you are creating a solution, it is usually important to ensure field consistency so that the solution works in all possible scenarios.

The following section discusses Outlook behavior in various circumstances to assist you in understanding where fields exist, how Outlook creates them dynamically, and how you can determine where the fields actually are so you can determine how user-defined fields may be impacting your solution.

Adding User-Defined Fields

When you use the Field Chooser dialog box to create a field, either while in a view or while in design mode of a form, the field will be added to the folder. If you are in form design mode and then drag the field onto the form, the field will be added to the item and Outlook will automatically create a control and bind the field to the control.

If you are in form design mode and right-click a control to access its properties, you can create a new user-defined field by clicking the New button on the Value tab of the Properties dialog box. Creating a field in this manner will result in the new field being added to both the item and the folder.

Fields can also be introduced into a folder solution by copying or moving an item from another folder into the solution folder. Fields can also be introduced if you programmatically create fields dynamically or as part of a solution to import items into a folder.

It is important to keep in mind that there are scenarios that can cause user-defined fields to not exist in one or more of the three locations (folders, items, published forms), and this can adversely affect a solution.

The following table summarizes Outlook behavior when creating new user-defined fields.

Table 4. Behavior of Outlook with new user-defined fields

Action Field Added To
Using New button on Field Chooser dialog box in a view Folder
New button on Field Chooser dialog box in form design mode Folder
Using the New button on the All Fields tab of an item while it is in design mode Folder
Dragging field from Field Chooser dialog box to form Item
Using New button on the Value tab in the Properties dialog box of a control when binding the control to a field Item and Folder

Deleting User-Defined Fields

Use the following table to determine how to delete a user-defined field that exists in the following locations:

Table 5. Deleting user-defined fields

To Delete Field From Do this
Folder Open the custom form for the folder in design mode by pointing to Forms on the Tools menu, and then clicking Design a Form. In the Look in drop-down list, click the name of the folder, click the form name, and then click Open. Use the Field Chooser dialog box and set the list to show User-defined fields in <folder name>. Click the name of the control and then click the Delete button.
Item Open the custom form in design mode (see the previous row). Use the All Fields tab of the item to click the field, and then click Delete.
Form (This procedure assumes that the custom form is the default form opened when the user create a new item for the folder. To see how to do this, see the section Message Classes.) Open the custom form in run-time mode by clicking the New button on the Standard toolbar. Delete the field using the All Fields tab of the form, and then re-publish the form.

Determining Where User-Defined Fields Exist

Use the following table to determine where a field exists:

Table 6. Determinings where a field exists

To Verify Fields Are In Do This
Folder Use the Field Chooser dialog box and set the list to show User-defined fields in <folder name>.
Item Open the custom form in design mode and then using the All Fields tab of the item and set the list to show User-defined fields in this item. Note that you may want to temporarily display this page of the form while you are developing the solution. If you created the field but it does not show up on the All Fields tab, you may not have published the form after adding the field. For the procedures to publish the form, see the section Saving, Publishing, and Distributing Forms.
Form (This procedure assumes that the custom form is the default form opened when the user create a new item for the folder. To see how to do this, see the section Message Classes.) Open the custom form in run-time mode by clicking the New button on the Standard toolbar, and then check the All Fields page of the form. If you created the field but it does not show up on the All Fields tab, you may not have published the form after adding the field. For the procedures to publish the form, see the section Saving, Publishing, and Distributing Forms.

Possible Issues After Adding User-Defined Fields

Here are some of the symptoms you may see when user-defined fields are not consistent in a folder-based solution:

  • Items become one-offs because fields are added to them dynamically. Typical symptoms include the size of the items increasing, the Message Class field of an item reverting back to the standard message class for that type of item, and the user receives the "Enable/Disable" macro virus warning when opening a one-off item.
  • Specific aspects of the Outlook object model may fail because they are designed to expect fields to exist in a particular location. We will discuss this further on in this article and provide an example of how code can fail.
  • General programming logic problems and errors if your Outlook object model code assumes that fields exist in a particular place and they are not there.

Best Practices When Working with Fields

There are a variety of best practices that should be taken into account when working with fields in a custom form solution.

Avoid creating items until the solution is finished

Ideally, you should create a custom form that contains all of the user-defined fields that you need for the solution. When that is done, you can publish the form to the folder and all items that are created in the folder will be based on the custom form and therefore inherit the user-defined fields. This scenario ensures that each user-defined field exists in all locations and therefore the solution has consistency.

If you create items for testing your solution as you develop it, you should delete these items and create new ones if you later add user-defined fields to the form.

If you must add fields into a folder solution after you created the items, consider making sure that all of the items get the new fields added to them. You can do this either programmatically (Item.UserProperties.Add), or by displaying the items in a table view, adding the user-defined field to the view, temporarily adding a value into the field so that Outlook adds the field into the item dynamically, and then deleting the temporary values.

When importing items, use Visual Basic for Applications

If you will be importing data into a folder as items, you cannot create the user-defined fields in the items using the Outlook Import/Export utility. If you use the utility to import the items, consider creating a procedure that will programmatically add all of the user-defined fields in the item or consider creating a procedure that will programmatically create and import all of the items.

Fields added to an item only if they have a value

Under various circumstances you can see different behavior depending on whether or not a field contains a value. For example, if you open a new contact item and create a new user-defined field on the All Fields page of the form, the field will be added to the folder, but the field will not be listed under User-defined fields in this item until you enter a value into the field. In other words, in scenarios where the fields exist in a folder but not an item, Outlook will automatically add the field to an item if the field is given a value. This can also happen when entering data into a table view using the in-cell editing feature.

Copying or moving items

If you copy or move items from one folder to another, the user-defined fields in the items will be moved but the fields will not be associated with the new destination folder. Depending on what you are trying to accomplish, you can either make a copy of the entire folder instead of moving individual items, or you can use the Field Chooser dialog box to redefine the user-defined fields in the new folder.

You can use the Find or Restrict methods in the Outlook object model to find all items in a folder that match certain criteria. For example, you may want to get all items where the OrderDate field (a user-defined field) value is sometime this year. For these methods to work, the user-defined field must exist in the folder, not just the items in the folder. You can use the Field Chooser dialog box to create a new field to match the one you are working with in code. You can also use the AddToFolderFields argument for the UserProperties.Add method to make sure that when you add a field into an item it is also added into the folder set of fields.

Try This: Working with User-Defined Fields

The following is a series of steps that create a sample folder-based solution. We will continue to build on our existing Vet Contacts solution.

Create and publish a simple custom contact form

The initial form will keep track of the customer's pet names and types.

  1. Select the Vet Contacts folder and open a new contact form. This is the Customer form we created previously.
  2. On the Tools menu, point to Forms, and click Design This Form.
  3. Click the Pet Info tab of the form. Delete any controls that have previously been added to this form page.
  4. On the Field Chooser dialog box, change the dropdown to show User-defined fields in folder.
  5. Click the New button and in the Name field type Pet Name. Click OK.
  6. Again click the New button and in the Name field type Pet Type. Click OK.
  7. Drag both the Pet Name and Pet Type fields to the Pet Info tab of the form.
  8. Re-publish the form as Customer_Pet_Info.
  9. Close the item and do not save changes.

In the above steps, when the fields were dragged from the Field Chooser dialog box to the form, Outlook propagated the fields to the item. Since the item was published as a form, the fields also now exist in the published form.

We now have the fields existing in the folder and in the form. Because all new items will be based on the published form, all items will also have the two user-defined fields. This is an ideal scenario, because the user-defined fields will now exist in every possible location when working in this folder.

Add the Message class and size to the view

The following steps will set up a table view and add fields to the view so that is is easier to monitor Outlook field behavior:

  1. Click into the Vets Contacts folder.

  2. On the View menu, point to Current View, and then click Define Views.

  3. In the Define Views dialog box (see Figure 6), click New.

    Click here for larger image

    Figure 6. Define Views dialog box (click picture for larger image)

  4. In the Create a New View dialog box (see Figure 7), type Veterinarian contacts into the Name of new view box, make sure Table is selected in the Type of view list, and then click OK.

    Aa155722.odc_olcustfrm107(en-us,office.10).gif

    Figure 7. Create a New View dialog box

  5. In the View Summary dialog box (see Figure 8), click Fields.

    Aa155722.odc_olcustfrm108(en-us,office.10).gif

    Figure 8. View Summary dialog box

  6. In the Show Fields dialog box (see Figure 9), remove all of the fields in the Show these fields in this order list.

    Click here for larger image

    Figure 9. Show Fields dialog box (click picture for larger image)

  7. Make sure All Contact fields is displayed in the Select available fields from drop down list. From the Available fields list, move the File As, Company, Message Class, and Size fields to the Show these fields in this order list, and then click OK.

  8. Click OK in the View Summary dialog box.

  9. In the Define Views dialog box, make sure that Veterinarian contacts is selected, and then click Apply View.

    Note  The Message Class and Size fields are being added for illustrative purposes. It is often advisable to display these fields when developing a folder solution.

  10. Adjust the width of your columns as needed.

Create two contact items for customers

  1. Click the Vets Contact folder, right-click, and then click Properties.

  2. On the General tab, click Customer_Pet_info in the When posting to this folder, use drop down list (see Figure 10), and then click OK.

    Aa155722.odc_olcustfrm110(en-us,office.10).gif

    Figure 10. Vets Contacts Properties dialog box

  3. Click the first toolbar button (New Contact) to launch a new contact item.

  4. Type Alan Adams as the Full Name, then click the Pet Info page and type Allie for the Pet Name and Alligator for the Pet Type (see Figure 11). Click Save and Close.

    Aa155722.odc_olcustfrm111(en-us,office.10).gif

    Figure 11. Pet Info page of the Vet Contact form

  5. Click the first toolbar button to launch a new contact item.

  6. Type Bob Barry as the Full Name, then click the Pet Info page and type Babs for the Pet Name and Baboon for the Pet Type. Click Save and Close.

At this point the two user-defined fields (Pet Name and Pet Type) are in the published form (Customer_Pet_Info) in the Vets Contacts folder, and in the two items that were just created in the folder.

Add an additional field to the Published form

Add an additional field to track whether or not each pet has been neutered.

  1. Click the first toolbar button to launch a new contact item.

  2. On the Tools menu, point to Forms, and click Design This Form.

  3. Click the Pet Info page.

  4. In the Field Chooser dialog box, click New, and type Neutered as the name of the new field.

  5. Click to select Yes/No in the Type list, leave the Format as Icon, and click OK.

  6. Drag the new Neutered field from the Field Chooser dialog box to the form page. Outlook automatically creates a check box control for the Yes/No field (see Figure 12).

    Aa155722.odc_olcustfrm112(en-us,office.10).gif

    Figure 12. Neutered field on the Customer_Pet_Info form

  7. On the Tools menu, point to Forms, and click Publish Form. If the Publish Form As window appears, select Customer_Pet_Info in the item list to populate the Display Name and Form Name boxes, and click Publish.

  8. Close the item and do not save the changes.

Add the new field to the view

  1. On the View menu, point to Current View, and then click Customize Current View.
  2. In the View Summary dialog box, click Fields. Make sure that User-defined fields in folder is displayed in the Select available fields from list, and then move the Neutered field to the other fields in the Show these fields in this order list.
  3. Arrange the fields as desired and click OK twice to close the open dialog boxes.

At this point the Neutered field exists in the folder and in the published form. Even though the two existing items created earlier were never directly exposed to the Neutered field, they are linked to the published form definition, so the Neutered field is treated as if it actually exists in the item. To verify this, open the contact item for Alan Adams by double-clicking on the item. Click the All Fields page of the form and then set the list to show User-defined fields in this item. Note that all three user-defined fields are listed as being in the item (see Figure 13) and then close the item.

Aa155722.odc_olcustfrm113(en-us,office.10).gif

Figure 13. All Fields page for customer Alan Adams

Note that in the table view the two items do not have a box in the column for the Neutered field. Typically columns for Yes/No fields have an empty box if the Yes/No field is False and a checked box if the field is True. In the current scenario, there is no box, indicating that fields are not actually in the item but are coming from the published form. This visual discrepancy may make it appear as though the fields are not part of the item, and while technically they are not, for practical purposes they are available.

Add an additional field to the folder

Now you will add an additional field for the neutering date but not use the published form to do so. We will also look at another method to add fields to a view.

  1. Make sure the Customer_Pet_Info form is closed.
  2. If the Advanced toolbar is not already displayed on the main Outlook screen, point to Toolbars on the View menu of the Outlook window, and click Advanced.
  3. Click the Field Chooser icon of the toolbar.
  4. In the Field Chooser dialog box, click New, type Neuter Date as the name of the new field, select Date/Time in the Type list, and select a date format that fits the MM/dd/yyyy format. For example, 11/21/2002. Click OK.
  5. Drag the Neuter Date field into the view column header just after the Neutered column, and close the Field Chooser dialog box.

The field is added to the folder, but does not exist in the published form or in either of the two items in the folder (double-click either item, click the Pet Info tab, and note that the Neuter Date field is not present).

Create a third contact item

  1. Click the first toolbar button to launch a new contact item.
  2. Enter Ozzie OneOff as the Full Name, click the Pet Info page and enter Otto for the Pet Name and Ostrich for the Pet Type. Leave the Neutered check box cleared.
  3. Click Save and Close.

Now set a value for the Neuter Date in this new item. In the Neuter Date column, click in the cell that contains the word None in the Ozzie OneOff row, and type a date. After you type the date, press TAB to have Outlook store the value in the field. Because you typed a value in the field in the table view and the field did not actually exist in the item, Outlook added the field to the item dynamically (see Figure 14).

Aa155722.odc_olcustfrm114(en-us,office.10).gif

Figure 14. Neuter Date added to Ozzie OneOff item

This created a one-off item since the form definition of this item no longer matches that of the published form. Notice the size of the item increased and that the Message Class field reverted to IPM.Contact. This is no longer linked to the published form in the folder. Every time it is opened, it will use the internal form definition and ignore the published form.

Now assume you wish to copy the contacts into an Exchange public folder to share them, but instead of making a copy of the folder you want to create a new folder and drag the contacts into it. In these steps we will use a standard folder instead of an actual public folder.

Create a folder for the contacts

  1. Right-click the topmost folder in the Folder List view (usually labeled Outlook Today) and on the shortcut menu, click New Folder. In the Name box type Vet Contacts2 and in the Folder contains list, click to select Contact Items. Click OK.

  2. Click the first contact (Alan Adams) in the Vet Contacts folder to select it. Then press SHIFT and click the bottom contact (Ozzie OneOff) to select the three contacts in the folder.

  3. On the Edit menu, click Copy To Folder, select the Vet Contacts2 folder, and then click OK.

  4. Click the Vet Contacts2 folder. You can see that the three contacts are now copied into this folder.

  5. Switch to the Phone List view (point to Current View on the View menu, and then click Phone List).

  6. Display the Field Chooser dialog box and then set the drop down to show User-defined fields in folder.

    The user-defined fields are not defined for the Vet Contacts2 folder.

  7. Close the Field Chooser dialog box.

Create a programming example

Use the following Outlook Visual Basic for Applications (VBA) code to retrieve those pet owners who have dogs.

  1. Press ALT+F11 to access the Visual Basic Editor. If you experience problems opening the Visual Basic Editor or running this code, you may need to check your security setting by pointing to Macro on the Tools menu, and then clicking Security.

  2. In the Project Explorer window, double-click Project1, double-click Microsoft Outlook Objects, and then double-click ThisOutlookSession.

  3. Enter the following code into the code window:

    Sub GetDogOwners()
       Set MyFolder = ActiveExplorer.CurrentFolder
       Set Custs = MyFolder.Items
       Set DogCusts = Custs.Restrict("[Pet Type] = 'Ostrich'")
    End Sub
    
  4. With the insertion point in the code, press F5 to run the code. You will receive a run-time error:

    The property "Pet Type" is unknown.

  5. Close the Visual Basic Editor. If you are prompted later to save changes, you can click No.

  6. Now go back to the Vet Contacts2 folder and using the Field Chooser dialog box, create a new field named Pet Type, leaving the Type and Format set as Text. This will add Pet Type as a user-defined field to the set of fields available for the folder.

  7. Run the above code again. This time, the code does not return an error. Close the Visual Basic Editor. If you are prompted later to save changes, you can click No. We will not use this code again in this topic.

  8. Delete the Vet Contacts2 folder since we will not use it again.

This was a very lengthy Try This, but it is hoped that you will be able to take away a better understanding of how user-defined fields work. Knowing exactly where user-defined fields are located and how and when they are transferred from location to location often plays an important role in being able to understand Outlook's behavior and effectively develop a solution.

Using the Field Chooser

You have already had a small taste of using the Field Chooser dialog box. However, the Field Chooser dialog box serves several purposes in Outlook. You can use it to:

  • Add fields to a table view by dragging a field from the Field Chooser dialog box to the column heading.
  • Create and delete user-defined fields in a folder (not an item).
  • Add fields to a form (using drag and drop) while in design mode.
  • See which standard fields are a part of which type of form by changing the dropdown at the top of the Field Chooser dialog box.

As you have already seen, if you are working in a table view, the easiest way to access the Field Chooser dialog box is to right-click the column heading and then click Field Chooser on the shortcut menu. In addition, the Field Chooser dialog box is available on the Advanced toolbar. When you enter form design mode, the Field Chooser dialog box is displayed by default.

Here are some things to remember when using the Field Chooser dialog box:

  • When using a table view, the different sets of fields listed in the Field Chooser dialog box are dynamic. Any fields that are already displayed in the view will not be displayed in the Field Chooser dialog box.
  • The Field Chooser dialog box sometimes allows you to mix and match fields across different types of items, even though these fields may not be designed for use on other types of items. For example, you can use the Field Chooser dialog box to add a To field to a contact form.
  • The Field Chooser dialog box may allow you to create a user-defined field that matches the name of a standard field. This is not recommended, as it can cause various problems. User-defined fields should not have the same name as a standard field.

Field Types

Outlook provides a wide variety of field types to include Text, Yes/No, Date/Time, and keywords for use in the vast majority of solutions. The following table lists all of the data types Outlook supports and provides a description of how each should be used.

Table 7. Field types

Type Description
Text This type of field is designed to handle regular text.
Number Requires that all character be numbers, or associated with numbers. For example, "$34" and "1.2" are valid values; "9d" and "23%" are not.
Percent Always displays a percentage sign after the number. Both "34" and ".34" are considered "34%."
Currency Values are displayed with a dollar sign and can either be rounded to the nearest dollar or have both decimal places displayed.
Yes/No Used to stored Boolean data. Programmatically, Yes = -1 and No = 0.
Date/Time Behaves much like Date/Time fields in other applications. If a date field does not have a value, Outlook arbitrarily assigns 1/1/4501 as the date, because internally, all date fields need to have some value. When referring to dates via VBScript, they should be enclosed with pound signs (i.e. #1/1/4501#). Internally, if a date is empty (equal to 1/1/4501), Outlook displays "None" on a form.
Duration Used to show time expired. Specified in hours. This control has no inherent intelligence built in.
Keywords A keywords field is used to store list data and is most commonly associated with the Categories field that Outlook uses on all forms types. Visually, values are separated by a comma and a space, but programmatically, these values are stored in an Array data structure. For information on working with keywords from VBScript, see OL2002: Working with Keywords Fields from VBScript.
Combination Combination fields are used to combine information from two or more other fields. A contact's Full Name field is a good example of this. You can create a custom combination field called LastFirst that will display a person name as "Smith, John" by creating a combination field and setting the expression to "[Last Name], [First Name]".
Formula Formula fields are used to generate numbers based on other fields and functions, much like a formula in a spreadsheet program. For example, if you create a custom form that has user-defined field to store the length and width of a room, you could create a formula field that would automatically store the total square footage of the room by multiplying the length and width fields.

Validating Field Data

Validating data is often a critical part of any type of form solution, regardless of what type of application or form you are using. After a user enters data into a form, there is often a need to make sure that the data matches certain criteria before you allow it to be entered. For example, if a field is designed to store a phone number, you may want to ensure that the user entered at least seven digits.

In Outlook, you can require that any field, or any control that is bound to a field, has a value. You can also make sure that a number falls within a certain range, or that data falls within the current year. You can specify just about any other criteria for the field.

If the data does not meet your criteria, you can also show a custom message that instructs the user on how to fill in the field if their entries do not match the values you want.

Note  Some fields, like duration or date fields, always have a value and therefore the A value for this field is required checkbox is dimmed. Date fields that do not have a value are arbitrarily given a value of 1/1/4501 and therefore cannot be empty.

To require a value for a field or control:

  1. In design mode, right-click the field or control, and then click Properties.

  2. Click the Validation page.

  3. Select the A value is required for this field check box.

    Aa155722.odc_olcustfrm115(en-us,office.10).gif

    Figure 15. The Validation tab of the control's Properties dialog

It is possible to get into situations where you are not able to close an item because Outlook displays the following error message: A field on this form requires a value. For more information, see OL2002: ErrMsg: A Field on This Form Requires a Value.

To create a validation formula and message:

  1. With the form in design mode, right-click the field or control you want to create a validation formula for, and then click Properties.

  2. Click the Validation page.

  3. Select the Validate this field before closing the form check box.

  4. In the Validation Formula box, enter the formula you want to use to evaluate user input.

  5. In the Display this message if the validation fails box, type the text you want to appear in the message. The following table presents examples of sample validation formulas:

    Table 8. Validation formulas

    To Evaluate: Use:
    Numbers less than 50 <50
    Numbers greater than 50 >50
    Equal to 50 =50
    Any three characters ???

Try This: Field Validation

In this exercise, we will add field validation to the Pet Type field so that no one can set the field to turtle or Turtle.

  1. Open a new instance of the Customer_Pet_Info form and enter design mode (click Tools, point to Forms, and then click Design This Form).

  2. Click the Pet Info tab of the form.

  3. Right-click the Pet Type text box and click Properties. Click the Validation tab.

  4. Enable Validate this field before closing the form.

  5. In the Validation Formula box, type <> "turtle" and <> "Turtle".

  6. In the Display this message if the validation fails box, enter "Sorry, the vet is allergic to turtles! Enter another pet type or ask the patient to visit another vet." Be sure to put quotes around the message, otherwise you will get some real strange behavior (try it and see!).

  7. Re-publish the form to the folder.

  8. Close and do not save changes to the item.

  9. Launch a new instance of the form. Type Tad Tipsy as the name of the contact and then switch to the Pet Info page and enter Turtle as the Pet Type.

  10. Click the Save and Close button on the item's toolbar. The validation message is displayed preventing you from entering Turtle in the Pet Type field (see Figure 16).

    Click here for larger image

    Figure 16. The Validation tab of the control's Properties dialog (click picture for larger image)

  11. Close and do not save changes to this contact item.

Setting Initial Values on a Field

Oftentimes, you want a field to contain a default value, so that if the person initially filling out the form does not enter a value, there will be a value by default. There are generally three different ways to set an initial value on a field:

  • Use the Initial Value settings on a control's Property page. To do this, right-click the appropriate control, click Properties, select the Value tab, and enable the Set the initial value of this field to setting. Note that the control must be bound to a field in order for this setting to be enabled. Note that the Calculate this formula when I compose a new form option is selected by default.
  • Use VBScript in an Item_Open event to set the value of the field.
  • Enter a value into the field before publishing the form.

Using the Initial Value settings is the preferred approach if you are not using VBScript in the form already. You typically would not want to add code to the form to simply set some initial values.

Try This: Setting Initial Values

In this exercise, we will add an initial value to the Pet Type field to indicate that it is unknown when a new Customer form is launched.

  1. Open a new instance of the Customer_Pet_Info form and enter design mode.

  2. Switch to the Pet Info tab of the form.

  3. Click the New button on the Field Chooser dialog box. Type Initial Visit as the field name, set the Type to Date/Time and select a date format that fits the MM/dd/yyyy format. For example, 11/21/2002. Click OK.

  4. Drag the Initial Visit field onto the form page.

  5. Right-click the Initial Visit text box, and click Properties.

  6. Click the Value tab. Select the Set the initial value of this field to check box and in the box below, type =Now(). Click OK.

  7. Re-publish the form. Close and do not save changes to the current item.

  8. Open a new instance of the form and click the Pet Info tab.

    What is the value in the Initial Visit field?

    Answer: The current date (see Figure 17).

    Aa155722.odc_olcustfrm117(en-us,office.10).gif

    Figure 17. Initial Visit control on the Customer_Pet_Info form

Having Fields Automatically Update

In addition to setting initial values using the Initial Value settings, Outlook lets you automatically calculate or set a field based on the contents of other fields on the form. This works much like setting an initial value, but the field is automatically updated whenever any of the other fields are updated. When choosing this option, fields can be very dynamic, much like many of the default contact fields, such when the File As field is automatically updated whenever the other name fields change.

Try This: Automatically Updating Fields

In this exercise, we will add a Pet Owner field to the Pet Info tab of the form, and set it to automatically calculate based on the Full Name field. This way is is easy to see who owns the pet when you look at the pet information on the form.

  1. Open a new instance of the Customer_Pet_Info form and enter design mode.

  2. Click the Pet Info tab.

  3. On the Field Chooser dialog box, click the New button. Create a text field called Pet Owners and drag the field onto the Pet Info page of the form.

  4. Right-click the text box for the Pet Owners field and click Properties.

  5. Click the Value tab, select Set the initial value of this field to, and then type the following into the box below: [Full Name] & IIf( [Spouse]<> "" , " (" & [Spouse] & ")" , "").

  6. Select the Calculate this formula automatically option and then click OK.

  7. Re-publish the form. Close and do not save changes to the current item.

  8. Open a new instance of the form and enter a first and last name for the contact.

  9. Click the Details tab of the form and enter a name for the spouse.

  10. Click the Pet Info tab and notice the Pet Owners field.

    What is the value of the field?

    Answer: First Last (Spouse) (see Figure 18)

    Aa155722.odc_olcustfrm118(en-us,office.10).gif

    Figure 18. Pet Owners control on the Customer_Pet_Info form

  11. Close and do not save changes to this new contact item.