Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

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

Summary

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

For more information about how to do this when you are not using a Microsoft Access database, click the following article numbers to view the articles in the Microsoft Knowledge Base:

198561 How to create a combo box containing more than 25 items

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

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, click the following article number to view the article in the Microsoft Knowledge Base:

212536 How to run sample code from Knowledge Base articles in Office 2000

In your template, use the following 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

  1. In Microsoft Office Word 2003 and in earlier verions, point to Macro on the Tools menu, and then click Visual Basic Editor.

    In Word 2007, click the Developer tab, 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 inside the UserForm, and then click View Code on the shortcut menu. Add the following code to the Initialize event:

    Option Explicit
    'Define Variables

    Private Sub UserForm_Initialize()

    Dim dbDatabase As Database
    Dim rsNorthwind As Recordset
    Dim i As Integer
    Dim aResults()

    ' This code activates the Database connection. Change
    ' the path to reflect your database.
    Set dbDatabase = OpenDatabase("C:\My Documents\NorthWind.mdb")

    ' This code opens the Customers table. Change the Table
    ' to reflect the desired table.
    Set rsNorthwind = dbDatabase.OpenRecordset("Customers", dbOpenSnapshot)

    i = 0

    With rsNorthwind
    ' This code populates the combo box with the values
    ' in the CompanyName field.

    Do Until .EOF
    ComboBox1.AddItem (i)
    ComboBox1.Column(0, i) = .Fields("CompanyName")
    .MoveNext
    i = i + 1
    Loop

    End With
    End Sub
  4. On the Tools menu, click References.

  5. Click Microsoft DAO 3.6 Object Library.

  6. Click OK to close the References dialog box.

  7. Close the code window.

  8. 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.

  9. On the Controls tab of the toolbox, select ComboBox and place it on your UserForm. 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

    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.

  10. Close the code window.

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

    Private Sub Cmdclose_Click()
    End
    End Sub
  12. Close the code window.

  13. Right-click 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.

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 is 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, select the gocombobox macro from the Entry: named combo box.


    NOTE: Make sure the Bookmark name of your text form field is the same as the one you specified in step 9 of the "Create the UserForm Combo Box" procedure earlier in this article.

  5. On the Forms toolbar, click Protect Form.

  6. Save and close your template.

To use the template in Word 2003 and in earlier verions, click New on the File menu. Select your template and then click OK.

To use the template in Word 2007, click the Microsoft Office Button, click New, select your template, and then click Create.

A new document based on your template appears. The UserForm that contains the ComboBox with your items appears when you use the TAB key to move into the text form field.

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Was this information helpful?

What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×