OL97 VBScript: How to Populate a ComboBox with Your Contacts

ID: Q167240


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


SUMMARY

This article describes how you can use Visual Basic Scripting Edition (VBScript) version 2.0 or higher, and Microsoft Outlook 97 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/support/supportnet/overview/overview.asp
NOTE: This example does not work with VBScript, version 1.0. You must install VBScript, version 2.0 or higher, 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:
Q167138 OL97: 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/scripting/vbscript
This example contains four steps.
  1. Create a new Contact form and place a ComboBox1 control on the form.


  2. Type 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. Open the new form and expose the ComboBox list containing your contacts.


Create the Form and ComboBox1 Control

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


  2. On the Tools menu, click "Design Outlook Form."


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


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


  5. On the Form menu, click Rename Page, and in the Page name box, type TabName and click OK.

    NOTE: The VBScript code uses this name.


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


Create the VBScript Code to Populate ComboBox1

  1. On the Form menu, click View Code, to open 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 combobox is on
        Set objFormTab = Item.GetInspector.ModifiedFormPages("TabName")
        ' Set the object referring to the combobox
        Set objCombo = objFormTab.Controls("ComboBox1")
        ' Loop through all of the contacts
        For Each Contact in objAllContacts
          ' Add the current contact's fullname to the combobox
          objCombo.AddItem Contact.FullName
        Next
        End Sub 


  3. On the Script Editor File menu, click Close.


  4. On the form Tools menu, click "Design Outlook Form."


Publish the New Contact Form

  1. On the form File menu, click "Publish Form As."


  2. In the Form name box, type a name for your new form and click Publish In.


  3. In the "Set Library To" box, click "Folder Forms Library."


  4. In the folder list, click to select your Contacts folder, and then click OK.


  5. Click Publish to publish the form in your Contacts folder.


  6. On the File menu, click Close to close the form.


Using the New Contact Form

  1. Open your Contacts folder.


  2. On the Contacts menu, click the new form name to open your newly published form.


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


On the TabName page of the form, you will see a single ComboBox. If you click to display the ComboBox 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 97, please see the following articles in the Microsoft Knowledge Base:

Q166368 OL97: How to Get Help Programming with Outlook

Q170783 OL97: Q&A: Questions about Customizing or Programming Outlook

Additional query words: OutSol OutSol97

Keywords : kbcode
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbinfo


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