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

ID: Q180911


The information in this article applies to:
  • Microsoft Outlook 98


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 a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.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 click the article number below to view the article in the Microsoft Knowledge Base:
Q182446 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, 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.


  3. 
          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 
  4. In the Script Editor click Close on the File 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 click the article numbers below to view the articles in the Microsoft Knowledge Base:

Q180826 OL98: Resources for Custom Forms and Programming

Q182349 OL98: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol98 higher listbox list box

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


Last Reviewed: October 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.