How to populate a list box or a combo box control on an custom form in Outlook 2002 (290819)



The information in this article applies to:

  • Microsoft Outlook 2002

This article was previously published under Q290819
For a Microsoft Outlook 97 version of this article, see 232342.

For a Microsoft Outlook 98 version of this article, see 231164.

For a Microsoft Outlook 2000 version of this article, see 232341.

SUMMARY

This article describes how to populate a list box or combo box control on an Outlook custom form.

MORE INFORMATION

There are a variety of ways to populate a list box or combo box on an Outlook form. The controls can be populated either at design-time or at run-time.

Populating List Boxes and Combo Boxes at Design-Time

If you have a predetermined and unchanging set of possible values, you can populate list boxes and combo boxes when designing the form. The following steps illustrate how to set up a simple list box control on an Outlook form.
  1. On the Tools menu, point to Forms, and then click Design a Form.
  2. In the Standard Forms Library, click Message, and then click Open.
  3. Click the (P.2) tab.
  4. On the Form menu, click Control Toolbox. On the Control Toolbox, drag a ListBox onto the form.
  5. Right-click the list box and then click Properties. On the Value tab, click New. Type ListBoxField for the name and click OK. In the Possible Values field, type Blue;Green;Red;Yellow, and then click OK.
  6. On the Form menu, click Run This Form.
Click the P.2 page of the form. You can then select any item in the list box, and the selected value will be stored in the ListBoxField user-defined field.

NOTE: In step 5, instead of using a Text field, you can use a Keywords field to store the data for a list box (not a combo box). When a list box is bound to a Keywords field, check boxes will appear next to the possible values in the list box and the user can make multiple selections. All of these selections are stored in the multi-value Keywords field. However, Keywords fields are limited to 255 characters, so this type of field should be used only if the user will be selecting from a relatively few possible values.

For additional information about this limitation of the Keywords field, click the article number below to view the article in the Microsoft Knowledge Base:

222475 OL2002: ListBox Bound to Keywords Field Unexpectedly Changes

Dynamically Populating List Boxes and Combo Boxes at Run-Time

Many solutions require that the possible values in a list box or combo box be up-to-date or refreshed regularly. The values are typically stored in some other location and need to be retrieved every time the item is opened. To accomplish this, you can add Visual Basic Scripting Edition (VBScript) code to the form and use an Item_Open event to run code whenever an item is opened. This code will fill the list box or combo box with possible values.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: There are various ways in which you can use VBScript to populate a list box or combo box.

PossibleValues Property

If you have a relatively simple list box or combo box to work with, you can automate the design-time steps by using the PossibleValues property of the control. This property is unique to Outlook and should be set to a semicolon-delimited string, similar to the design-time steps earlier. To see how the PossibleValue property can be implemented, follow these steps:
  1. On the Tools menu, point to Forms, and then click Design a Form.
  2. In the Standard Forms Library, click Message, and then click Open.
  3. Click the (P.2) tab.
  4. On the Form menu, click Control Toolbox. On the Control Toolbox, drag a ListBox onto the form.
  5. Right-click the list box and then click Properties. On the Value tab, click New. Type ListBoxField2 for the name and click OK. Leave the Possible Values field blank, and then click OK.
  6. On the Form menu, click View Code. Enter the following VBScript code and then close the Script Editor.
    Sub Item_Open()
    
       ' Sets the name of page on the form (P.2)
       Set FormPage = Item.GetInspector.ModifiedFormPages("P.2")
    
       ' Sets Control to a list box called ListBox1.
       Set Control = FormPage.Controls("ListBox1")
    
       ' Assign values to the list box.
       Control.PossibleValues = "Blue;Green;Red;Yellow"
    
    End Sub
    					
  7. On the Form menu, click Run This Form.
Click the P.2 page of the form. You can then select any item in the list box and the selected value will be stored in the ListBoxField2 user-defined field.

IMPORTANT When you set the PossibleValues property in Outlook 2000 or later, the form becomes a one-off. By default, Outlook 2002 does not run VBScript in one-off forms. To avoid this, use the AddItem method or the List property to populate the control. These two approaches do not result in a one-off form. For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

290657 Description of form definitions and one-off forms in Outlook 2002

290500 Description of the developer-related e-mail security features in Outlook 2002

AddItem Method

The AddItem method is typically used when you are looping through a series of data. As you loop through the data, you add items to the list box or combo box one at a time.

For an example of using the AddItem method to populate a combo box, please see the following article in the Microsoft Knowledge Base:

290818 OL2002: How to Populate a Combo Box with Your Contacts

List Property

List boxes and combo boxes have a List property that allows you to specify an array which contains the data the control should use. If your programming solution is already using arrays to store data, using this approach can provide the simplest programming solution. Also, one key advantage to using the List property is that if you wish to populate the list box or combo box with multi-column data, the List property supports multi-dimensional arrays to accomplish this.

To populate a two-column list box with data from your Contacts folder, follow these steps:
  1. On the Tools menu, point to Forms, and then click Design a Form.
  2. In the Standard Forms Library, click Message, and then click Open.
  3. Click the (P.2) tab.
  4. On the Form menu, click Control Toolbox. On the Control Toolbox, drag a ListBox onto the form. Make sure the list box is sized large enough to fill most of the form page.
  5. On the Form menu, click View Code. Enter the following VBScript code and then close the Script Editor.
    Sub Item_Open()
    
       Dim FullArray()
    
       ' Sets the name of page on the form (P.2)
       Set FormPage = Item.GetInspector.ModifiedFormPages("P.2")
    
       ' Sets Control to a list box called ListBox1.
       Set Control = FormPage.Controls("ListBox1")
    
       ' Get the default Contacts folder
       Set ConFolder = Application.Session.GetDefaultFolder(10)
    
       ' Get the items in the folder
       Set ConItems = ConFolder.Items
    
       ' Get the number of total items in the Contacts folder
       NumItems = ConItems.Count
    
       ' Resize array to handle total number of item in the folder
       ReDim FullArray(NumItems-1,2)
    
       ' Loop through all of the items in the Contacts folder,
       ' filling the array with sample data and keeping track
       ' of the number of contacts found.
       NumContacts = 0
       For I = 1 to NumItems
          Set itm = ConItems(I)
          If Left(itm.MessageClass, 11) = "IPM.Contact" Then
             NumContacts = NumContacts + 1
             FullArray(NumContacts-1,1) = itm.FullName
             FullArray(NumContacts-1,2) = itm.CompanyName
          End If
       Next
    
       ' Set the control to handle 2 data columns
       Control.ColumnCount = 3
    
       If NumItems = NumContacts Then
          ' They are all contacts, so use the FullArray
          Control.List() = FullArray
       Else
          ' There's some distribution lists, so use the smaller
          ' ConArray to eliminate extra blank values in the list box
          Dim ConArray()
          ReDim ConArray(NumContacts-1,2)
          For I = 0 to NumContacts - 1
             ConArray(I,1) = FullArray(I,1)
             ConArray(I,2) = FullArray(I,2)
          Next
          Control.List() = ConArray
       End If
    
    End Sub
    					
  6. On the Form menu, click Run This Form.
Click the P.2 page of the form. The list box will be populated with the data from your default Contacts folder.

REFERENCES

For additional information about available resources and answers to commonly asked questions about Microsoft Outlook solutions, click the following article number to view the article in the Microsoft Knowledge Base:

287530 OL2002: Questions About Custom Forms and Outlook Solutions


Modification Type:MinorLast Reviewed:3/15/2006
Keywords:kbForms kbProgramming kbDSWNET2003Swept kbhowto KB290819