KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q194026: WD97: How to Create a Combo Box with More Than 25 Items

Article: Q194026
Product(s): Word 97 for Windows
Version(s): 
Operating System(s): 
Keyword(s): kbdta kbdtacode kbmacroexample kbwordvba kbfield word97 kbform kbForms
Last Modified: 13-MAY-2002

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Word 97 for Windows 
-------------------------------------------------------------------------------

SUMMARY
=======

This article describes how to create and use a UserForm Combo box as an Entry
macro for a text form field. This macro can be used as a workaround for the
25-item limitation in drop-down form fields.

NOTE: <A0>For additional information about how to do this when you are using a
Microsoft Access database, click the article number below to view the article in
the Microsoft Knowledge Base:

  Q253552 WD97: How to Create a Combo Box Containing More Than 25 Items from an
  Access Database

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
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:

  http://www.microsoft.com/partner/referral/

For more information about the support options that are available and about how
to contact Microsoft, visit the following Microsoft Web site:

  http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

For more information about how to use the sample code in this article, click the
article number below to view the article in the Microsoft Knowledge Base:

  Q173707 OFF97: How to Run Sample Code from Knowledge Base Articles

In your template, follow these steps.

NOTE: To close a window (for example, a code window or the Properties window),
click the X in the upper-right corner of the window.

Create the User Form Combo Box
------------------------------

1. On the Tools menu, point to Macro and then click Visual Basic Editor.

2. In the Project Window, select your TemplateProject.

  NOTE: If the Project window does not appear, click Project Explorer on the
  View menu.

3. On the Insert menu, click UserForm. A new UserForm and the Controls Toolbox
  should appear. Right-click the UserForm and then click View Code on the
  shortcut menu. Add the following code to the Initialize event:

  Private Sub UserForm_Initialize()

     ComboBox1.ColumnCount = 1
     Dim MyArray(3)

     MyArray(0) = "Zero"
     MyArray(1) = "One"
     MyArray(2) = "Two"
     MyArray(3) = "Three"

     'Load data into ComboBox
     ComboBox1.List() = MyArray

  End Sub

  Close the code window.

  NOTE: MyArray() can contain as many or few items as you need. The array is not
  limited to 25 items like the drop-down form field. Make sure the DIM
  MyArray() statement contains the highest array number of the items contained
  in your array (not the total number of array items) from step 3 of the
  "Create the User Form Combo Box" procedure earlier in this article. For
  example, if you add a fifth item (MyArray(4) = "Four") to the example array
  in step 3 of the "Create the User Form Combo Box" procedure, make sure you
  change the DIM MyArray(3) statement to DIM MyArray(4).

4. Right-click the UserForm and then click Properties on the shortcut menu. On
  the Alphabetic tab, rename (Name) to frmcombo and then rename Caption to
  Microsoft Word. Close the UserForm Properties window.

5. On the Controls tab of the toolbox, select ComboBox and place it on your User
  Form. Right-click the ComboBox control and then click View Code on the
  shortcut menu. Change the ComboBox code to:

  Private Sub ComboBox1_Change()

     ActiveDocument.FormFields("Text1").Result = ComboBox1.Value

  End Sub

  Close the code window.

  NOTE: Text1 is the bookmark name of the Text Form Field that you insert into
  your template in step 2 of the "Create the Text Form Field" procedure of this
  article.

6. On the Controls tab of the toolbox, select the CommandButton and place it on
  your User Form as a Close button. Right-click the CommandButton and then
  click View Code on the shortcut menu. Change the CommandButton code to:

  Private Sub Cmdclose_Click()

     End

  End Sub

  Close the code window. Right-click the CommandButton and click Properties on
  the shortcut menu. On the Alphabetic tab, rename (Name) to Cmdclose and then
  rename Caption to Close. Close the CommandButton Properties window.

The UserForm Combo box is now complete. Proceed to the "Create the Entry Macro"
procedure.

Create the Entry Macro
----------------------

1. In the Project Window, select your TemplateProject.

2. On the Insert menu, click Module. A blank module code sheet appears.

3. Type in the following code:

  Sub gocombobox()

     frmcombo.Show

  End Sub

The entry macro is now complete. On the File menu, click Close and Return to
Microsoft Word.

Create the Text Form Field
--------------------------

1. In your template, on the View menu, point to Toolbars and then click Forms.

2. Position your insertion point where you want the result of the drop-down list
  inserted. On the Forms toolbar, click Text Form Field (the first button on
  the left). The Text Form Field will be inserted into your template as gray
  shading.

  NOTE: If you see the {FORMTEXT} field, press ALT+F9 to turn the field codes
  off.

3. Right-click the Text Form Field and then click Properties on the shortcut
  menu.

4. In the Text Form Field Options dialog box, under the Run macro on section,
  click the down arrow in the Entry box and select the gocombobox macro.

  NOTE: Make sure the Bookmark name of your Text Form Field is the same as you
  specified in step 4 of the "Create the User Form Combo Box" procedure earlier
  in this article.

5. On the Forms toolbar, click Protect Form.

6. Save and close your template.

To use your template, click New on the File menu. Select your template and then
click OK. A new document based on your template appears. The UserForm containing
the ComboBox with your items appears when you tab into the Text Form Field.

Additional query words: vba

======================================================================
Keywords          : kbdta kbdtacode kbmacroexample kbwordvba kbfield word97 kbform kbForms 
Technology        : kbWordSearch kbWord97 kbWord97Search kbZNotKeyword2
Version           : :
Issue type        : kbhowto

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.