Microsoft KB Archive/306258

= How to create a combo box that contains more than 25 items in Word 2002 =

Article ID: 306258

Article Last Modified on 7/27/2006

-

APPLIES TO


 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q306258



For a Microsoft Word 2000 version of this article, see 198561.

For a Microsoft Word 97 version of this article, see 194026.

For a Microsoft Word 98 version of this article, see 194027.



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: For additional information about how to do this when you are using a Microsoft Access database, click the following article number to view the article in the Microsoft Knowledge Base:

253542 WD: How to Create a Combo Box with More Than 25 Items from a Microsoft Access Database



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

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:

290140 OFFXP: How to Run Sample Code from Knowledge Base Articles

In your Word 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 UserForm Combo Box
 On the Tools menu, point to Macro and then click Visual Basic Editor. In the Project Window, select your TemplateProject.

NOTE: If the Project Window does not appear, click Project Explorer on the View menu.  On the Insert menu, click UserForm. A new UserForm and the Controls toolbox appear. Right-click the UserForm, and then click View Code. Add the following code: Private Sub UserForm_Initialize

ComboBox1.ColumnCount = 1

'Load data into ComboBox

ComboBox1.List = Array(&quot;Zero&quot;, &quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;)

End Sub

Close the code window.  Right-click the UserForm, and then click Properties on the shortcut menu. On the Alphabetic tab, rename (Name) to UserForm, and then rename Caption to Microsoft Word. Close the UserForm Properties window.  On the Controls tab of the toolbox, select ComboBox and then place it on your UserForm. Right-click the ComboBox control, and then click View Code on the shortcut menu. Change the ComboBox code to the following Private Sub ComboBox1_Change

ActiveDocument.FormFields(&quot;Text1&quot;).Result = ComboBox1.Value

End Sub and then close the code window.

NOTE: Text1 is the bookmark name of the Text Form Field that you will insert into your template in step 2 of the &quot;Create the Text Form Field&quot; procedure later in this article.   On the Controls tab of the toolbox, select the CommandButton, and then place it on your UserForm as a Close button. Right-click the CommandButton, and then click View Code on the shortcut menu. Change the CommandButton code to the following Private Sub Cmdclose_Click

Unload Me

End Sub and then close the code window. Right-click the CommandButton, and then 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. </li></ol>

The UserForm Combo box is now complete. Proceed to the &quot;Create the Entry Macro&quot; section of this article.

Create the Entry Macro
<ol> In the Project window, select your TemplateProject.</li> On the Insert menu, click Module. A blank module code sheet appears.</li>  Type the following code: Sub gocombobox

UserForm.Show

End Sub </li></ol>

The entry macro is now complete. On the File menu, click Close and Return to Microsoft Word. Proceed to the &quot;Create the Text Form Field&quot; section of this article to create the form field in your Word template.

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 is inserted into your template as gray shading.

NOTE: If you see the {FORMTEXT} field, press ALT+F9 to turn off the field codes.
 * 1) Right-click the Text Form Field, and then click Properties on the shortcut menu.
 * 2) In the Text Form Field Options dialog box, under the Run macro on section, click the arrow in the Entry box, select the gocombobox macro, and then click OK.

NOTE: Make sure the Bookmark name of your Text Form Field is the same as what you specified in step 4 of the &quot;Create the UserForm Combo Box&quot; section earlier in this article.
 * 1) On the Forms toolbar, click Protect Form.
 * 2) Save and close your template.

To use your template, click New on the File menu, and then click General Templates in the New Document task pane. In the Templates dialog box, select your template and then click OK. A new document based on your template appears. The UserForm containing the ComboBox with your items is displayed when you use the TAB key to move into the Text Form Field.

Additional query words: vba vbe vb dropdown drop-down

Keywords: kbhowto kbprogramming kbcontrol kbvba KB306258

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.