How to create a Userform

Article contributed by Doug Robbins

This example will step you through the process of creating a template that contains an autonew macro which, when you create a new document from the template, will cause a Userform to be displayed, into which you can enter some information that you want to appear in the document.

For example, when creating a letter or a fax; when you click on the OK button on the Userform, the information that has been entered into it  will be inserted into bookmarks located in the document at the places that you want the information to appear.

The example shows how you would deal with two pieces of information that you want to put into the document. At pertinent places in the following, comments have been added indicating where you would modify the code to deal with more pieces of information.

Step

Comments

1.

Create a template.

 See: Creating a Template – The Basics (Part I) and Creating a Template (Part II)

2.

Create two bookmarks in your template named Text1 and Text2”. To create each bookmark, select a single space so that when you turn on the option to see the bookmarks under Tools>Options>View, the bookmarks appear as [] rather than |. When you create bookmarks with a space selected like this, the text will be inserted inside the bookmark, rather than after it.

You can then use a cross reference to the text of the bookmark if you want the same information to appear in another location.

Add additional bookmarks for each piece of data) Use more descriptive names for the bookmarks if you like as it does make it easier to remember which is which and what goes where

3.

Go to the Visual Basic Editor (Tools>Macro>Visual Basic Editor)

Alternatively, you can hold down the Alt key and press F11

4.

With the Template selected in the Project Explorer, select UserForm from the Insert menu

A UserForm will appear in the right hand pane and the Controls toolbox will appear in the left hand pane.

If there are no other Userforms in the Template, it will by default be UserForm1. 

5.

From the Controls toolbox, select the TextBox control – ab| and then move the mouse cursor over to the UserForm, click and hold the Left mouse button and drag the cursor to make the TextBox the desired size. Repeat this step for a second TextBox or for as many as you require.

By default, the TextBoxes will be numbered TextBox1, TextBox2 ...TextBoxn.

You can change the names of the TextBoxes in the Properties window in the left hand pane. If you name them the same as the Bookmarks that you inserted into the template in Step 1, it makes it easier

6.

Dimension the TextBoxes by selecting and dragging as required for the text that you intend to be inserted into them 

7.

Add any labels that you want to have on the form to assist the user by clicking on the Label control – A in the Controls Toolbox and inserting them in the same way as for the TextBoxes.

8.

In the Controls Toolbox, click on the CommandButton control and insert one in the UserForm.

By default, this will be named CommandButton1

9.

Right Click on the CommandButton in the UserForm and select the View Codeitem from the menu.

10.

Enter the following code between the Private Sub CommandButton1_Click() and theEnd Sub

With ActiveDocument
    .Bookmarks("Text1").Range _
    .InsertBefore TextBox1
    .Bookmarks("Text2").Range _
    .InsertBefore TextBox2 
End With

UserForm1.Hide

Repeat the lines beginning with
.Bookmarks 
for eachbookmark.

Similarly, you can insert the same information into a second bookmark by inserting a line referring to the range of that bookmark

11.

Close the Visual Basic Editor     

12.

From the Tools Menu select Macro, then Macros, then with the Template selected in the Macros in: box, type the name autonew in the Macro name:box and click the Create button.

13.

Enter the following command in the module screen:

UserForm1.Show

14.

Exit from the Visual Basic Editor and save your template.

When you create a new document from the template, the autonew macro opens the user form for the user to input data, then when the command button is clicked, the code associated with that button inserts each piece of information into the respective bookmark.

This has been restricted to just enough to get you started.  There's lots more that you can do with UserForms. If you get stuck, post a question to the microsoft.public.word.vba.userforms newsgroup and someone will spring to your rescue..