OL98: How to Populate a Combo Box with Your Contacts (VBScript)

ID: Q180911

The information in this article applies to:

SUMMARY

This article describes how you can use Microsoft Visual Basic Scripting Edition (VBScript) version 2.0 or later and Microsoft Outlook 98 to populate a ComboBox control with the FullName field of all of your contacts.

MORE INFORMATION

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/default.asp

NOTE: This example does not work with VBScript version 1.0. You must install VBScript version 2.0 or later to use this example.

For additional information on how to determine your version of VBScript, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q182446
   TITLE     : OL98: General Information About Using VBScript with Outlook

The latest version of VBScript is available on the Microsoft Web site at the following address:

   http://www.microsoft.com/vbscript

This example contains four steps.

1. Create a new Contact form and place a ComboBox1 control on the form.

2. Enter the VBScript code that populates ComboBox1 with the FullName

   field of all of your contacts.

3. Publish the new form so that the Item_Open event runs when you open the
   new Contact form.

4. Use the new form to expose the combo box list containing your contacts.

Create the Form and ComboBox1 Control

1. On the Outlook File menu, point to New, and then click Contact.

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

3. Click the (P.2) tab in the form.

4. On the Form menu, click Display This Page.

5. On the Form menu, click Rename Page. In the Page Name box, type

   "TabName" (without the quotation marks), and then click OK.

   NOTE: The VBScript code uses this name.

6. On the Form menu, click Control Toolbox. Drag the ComboBox control to
   the form's TabName page.

Create the VBScript Code to Populate ComboBox1

1. On the Form menu, click View Code.

   This opens the Script Editor.

2. Type the following code in the Script Editor.

      Sub Item_Open

         ' Set the NameSpace object.
         Set objOLNS = Application.GetNameSpace("MAPI")

         ' Set the Contacts folder, which is in the MAPI NameSpace.
         Set objContactFolder = objOLNS.GetDefaultFolder(10)

         ' Set the collection of all Contact items.
         Set objAllContacts = objContactFolder.Items

         ' Set the object referring to the form page the combo box is on.
         Set objFormTab = Item.GetInspector.ModifiedFormPages("TabName")

         ' Set the object referring to the combo box.
         Set objCombo = objFormTab.Controls("ComboBox1")

         ' Loop through all of the contacts.
         For Each Contact in objAllContacts
            ' Add the current contact's full name to the combo box.
            objCombo.AddItem Contact.FullName
         Next
      End Sub

3. In the Script Editor click Close on the File menu.

4. In the form, click Design Outlook Form on the Tools menu.

Publish the New Contact Form

1. In the form, point to Forms on the Tools menu, and then click Publish

   Form As.

2. Confirm that the Look In is set to Outlook Folders and the Contacts
   folder is selected.

3. In the Display name box, type a name for the form, and then click
   Publish.

4. On the File menu, click Close. When prompted to save changes, click No
   so that a new item is not created.

Using the New Contact Form

1. Open your Contacts folder.

2. On the Actions menu, click the new form name to open your newly-

   published form.

3. In the new form, click the TabName tab.

4. On the TabName page of the form, you will see a single ComboBox control.

   If you click to display the combo box list, you will see a list
   Containing the full name of all of your contacts.

REFERENCES

For more information about creating solutions with Microsoft Outlook 98, please see the following articles in the Microsoft Knowledge Base:

   Article-ID: Q180826
   Title     : OL98: Resources for Custom Forms and Programming

   Article-ID: Q182349
   Title     : OL98: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol98 higher listbox list box

Keywords          : kbcode kbprg OffVBS 
Version           : WINDOWS:98
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: May 17, 1999