Getting Started with UserForms

The best way to get started with UserForms is to create a simple macro that opens a custom dialog box and uses it to display information. In this section, you'll create a music trivia macro that asks the user a simple question about a popular rock-and-roll instrument. Along the way, you'll learn the three fundamental steps of creating a Visual Basic UserForm: designing the user interface, setting properties, and writing event procedures.

The process used to create UserForms is identical in Microsoft Word, Microsoft Excel, and Microsoft PowerPoint. You'll find the same toolbox controls and property settings in each application.

Designing the User Interface

A UserForm is simply a custom dialog box that you create in the Visual Basic Editor by using programmable interface objects called toolbox controls. To open a UserForm in the Visual Basic Editor, you choose the UserForm command from the Insert menu. Each UserForm appears in a separate Project window in the Visual Basic Editor, and it's also listed in the Forms folder in Project Explorer. The first UserForm is named UserForm1, and subsequent UserForms are named UserForm2, UserForm3, and so on.

Whenever a UserForm is active in the Visual Basic Editor, a palette of toolbox controls also appears in a window, which allows you to add programmable interface objects to your UserForm. (See Figure 40-1.) If you have used a drawing program such as Paint, you have many of the skills you need to use toolbox controls. To build the interface objects, you click a control in the toolbox, and then you draw the interface object by dragging with the mouse. This task is usually a simple matter of clicking to position one corner of the object and then dragging to create a rectangle that's exactly the size that you want. After you create the object— a text label, for example— you can resize it by using the selection handles, or you can relocate it by dragging. You can also resize or move the UserForm itself to create a dialog box in exactly the size and location you want.

click to view at full size.

Figure 40-1. You create interface objects on your form using toolbox controls.

Buidling a Custom Dialog Box

Follow these steps to create a UserForm using the Label, Image, and CommandButton controls:

  1. Start Microsoft Word. (If Word is already running, open a new, blank document.)
    The MusicTrivia macro is located in the Chap61 document on the Running Office 2000 Reader's Corner page. The Guitar.bmp file (a supporting bit-mapped graphic) is also on the Reader's Corner page.

  • Choose Macros from the Macro submenu of the Tools menu.

    Word opens the Macros dialog box, the place where you create and run Visual Basic macros.

  • Type MusicTrivia in the Name text box, and then click the Macros In drop-down list box and select your new, blank document in the list. (In this example, the new document is named Document1.)
  • Click the Create button.

    Word starts the Visual Basic Editor and opens a new macro procedure named MusicTrivia in the Code window.

  • Type the following program statements to load and open the UserForm:

     Load UserForm1  UserForm1.Show 

    Every macro that opens a UserForm needs these two program statements to bring the UserForm into memory and display it. In this simple macro, you'll type only two lines in the macro. You'll type the remaining program statements into event procedures associated with the objects on the UserForm. (More about event procedures later.)

  • From the Insert menu, choose UserForm.

    The Visual Basic Editor opens a new UserForm in a window and displays the toolbox controls. The UserForm is named UserForm1.

  • Click the Label control in the toolbox, and then place the mouse pointer over the UserForm.

    The Label control is selected, and the mouse pointer changes to crosshairs when it rests on the form. The crosshairs are designed to help you draw the rectangular shape of a label. When you hold down the left mouse button and drag, the label object takes shape and snaps to the grid formed by the intersection of dots on the form.

  • TIP
    To learn the name of a control in the toolbox, hold the mouse pointer over the control until its tool tip appears.

  • Move the mouse pointer to the middle of the UserForm (near the left edge), hold down the left mouse button, and then drag down and to the right. Stop dragging and release the mouse button when your label object looks like the one in the following illustration.

    The purpose of a label object is to display formatted text on a UserForm. The first label object on a form is named Label1, and subsequent labels are named Label2, Label3, and so forth. You'll add text to the label object later.

  • Click the Label control in the toolbox again, and then create a second, smaller label object below the first one.

    Each label object on a UserForm maintains its own set of properties and methods. By creating two separate label objects, you'll be able to manipulate them individually using program code.

  • Click the Image control in the toolbox, and create a large, square image object on the right side of the form.

    The purpose of an image object is to display clipart, photographs, bitmaps, and other electronic artwork on a UserForm. (Specifically, an image object can display .ico, .wmf, .bmp, .cur, .jpg, and .gif files.) You'll use this image object to display a photograph of the musical instrument that demonstrates the answer to the musical trivia question.

  • Click the CommandButton control in the toolbox, and create a command button object at the bottom of your UserForm, on the left side.

    The purpose of a command button object is to create dialog box buttons on a UserForm. Typical command buttons include OK and Cancel, but you can also create your own button types.

  • Click the CommandButton control again, and then create a second command button object at the bottom of your UserForm, on the right side.

    You're finished creating objects on your UserForm. If the final dialog box doesn't look like the one shown in Figure 40-2, use the mouse to fine-tune the size and location of your objects.

  • Click the Save button on the Visual Basic toolbar, and then specify a filename for your new document.

  • Figure 40-2. A UserForm with five objects.

    Setting Properties

    After you create objects on your UserForm using toolbox controls, your next step is customizing the objects by using property settings. As you learned in Chapter 37, a property setting is a quality or characteristic of an object that can change as your macro runs. You can change the property settings for objects on a UserForm by using the Properties window at design time (when your macro is being built), or by using program code at run time (while your macro is executing).

    Follow these steps to set properties in the MusicTrivia macro's UserForm:

    1. Click the first label object on the UserForm.

      Before you can set a property for an object, you must select the object on the UserForm. When you select the first label object, its name (Label1) appears at the top of the Properties window in the Object drop-down list box.

    2. TIP
      To see more of the Properties window, double-click its title bar to display it in its fully expanded position. To return the Properties window to a docked position, double-click the title bar again.

  • In the Properties window, double-click the text (Label1) to the right of the Caption property to select it, and press Delete.

    The default text setting for the Caption property is deleted. Now enter a new caption.

  • Type What rock and roll instrument is often played with sharp, slapping thumb movements? and press Enter.

    The contents of the Label1 object on the form change to match your trivia question. Because the label object's WordWrap property is set to True by default, the text wraps inside the label object.

  • Click the Label2 object on the UserForm, and follow the same steps to change its Caption property to The Bass Guitar.
  • With the Label2 object still selected, click the Visible property, and change its setting to False.

    You'll keep the answer hidden until the first command button is pressed.

  • Click the Image1 object on the form.

    Now you'll set the PictureSizeMode, Picture, and Visible properties of the image object to display a photograph of a bass guitar when the user clicks a command button.

  • Click the PictureSizeMode property, and select 1-fmPictureSize ModeStretch in the drop-down list box.

    This property setting resizes artwork in an image box so that it fits exactly.

  • Click the Picture property, and then click the button containing three dots in the setting field.

    A dialog box appears prompting you to select a piece of artwork for the image box.

  • Browse to a picture file on your system, and then click Open.
  • We recommend that you use the Guitar.bmp graphic located on the Running Office 2000 home page, but you can also select a piece of artwork on your own computer. (You'll find several in the \Windows folder.)
    The Guitar.bmp graphic is located on the Running Office 2000 Reader's Corner page.

    If you select the Guitar.bmp file, you'll see the photograph of a hip Seattle bass player now in the image box. (Look closely— he's currently demonstrating the slap-bass technique.)

  • Click the Visible property for the image object, and set it to False. You'll keep the photograph hidden until the user clicks the first command button.
  • Now select the first command button object on the form, and change its Caption property to Answer.
  • Change the Caption property of the second command button to Quit.
  • Click the UserForm itself (not the title bar or an object), and then set the Caption property of the UserForm to Music Trivia.

    Your UserForm will look similar to the one shown in Figure 40-3.

  • You're finished setting properties for the macro.

    Figure 40-3. The completed user interface of the MusicTrivia UserForm.

    Writing Event Procedures

    The final step in creating a UserForm is writing the program code for the interface objects on the UserForm. Fortunately, most of the objects on a UserForm already know how to work when the macro runs, so you just need to add the final touches using a few carefully designed event procedures. An event procedure is a special routine that runs when an object on your form is manipulated at run time. (Technically, event procedures run when a specific event is triggered in the macro, such as a click, a double-click, or a drag-and-drop operation.) UserForm event procedures use the same Visual Basic macro language that you're familiar with, so you'll have little trouble figuring out what to do. Like Office macros, the trick to learning the ropes is understanding what the most important properties and methods do and then executing them in the proper sequence using program code.

    Using the Code Window to Write Event Procedures

    Follow these steps to write click event procedures for the two command button objects on the UserForm: CommandButton1 and CommandButton2.

    1. Double-click the CommandButton1 object (the button that has the Answer caption).

      The Visual Basic Editor opens the click event procedure for the CommandButton1 object in the Code window.

      click to view at full size.

    2. Type the following code between the Private Sub and End Sub statements:
    3.  Image1.Visible = True  Label2.Visible = True 

      These program statements make the Image1 and Label2 objects visible on the UserForm when the user clicks the Answer button.

    4. Click the Object drop-down list box in the Code window, and select the CommandButton2 object.
    5. Type the following statement between Private Sub and End Sub:

       Unload UserForm1 

      This line unloads the music trivia UserForm and closes the macro when the user clicks the Quit button.

      click to view at full size.

    6. TIP
      Use the Unload statement whenever you want to close a UserForm and return to the Word macro that opened it.

    7. Now click the Save button to save your UserForm and macro to disk.

    Running the Music Trivia Macro

    Congratulations, you have built your first UserForm. Now return to Word and run the macro. Follow these steps:

    1. Click the View Microsoft Word button on the Visual Basic Editor toolbar.
    2. Press Alt+F8, and double-click MusicTrivia.

      Alt+F8 is the keyboard shortcut to display the Macros dialog box. After you double-click the macro name, Office starts the macro and displays the UserForm on the screen. (Pretend you don't know the answer to our little puzzler.)

    3. Click the Answer button.

      The answer and photograph appear on the form, just as you stipulated in the CommandButton1 event procedure.

    4. Click the Quit button to close the macro.

      The UserForm unloads and the macro stops.

    If your macro didn't display the expected results, check the spelling of the statements in your event procedures, and check the property settings you made. If you forgot to set the Image1 and Label2 Visible properties to False, Visual Basic won't hide them when your macro starts.

    Running Microsoft Office 2000 Small Business
    Running Microsoft Office 2000
    ISBN: 1572319585
    EAN: 2147483647
    Year: 2005
    Pages: 228
    Authors: Michael Halvorson, Michael J. Young
    BUY ON AMAZON © 2008-2017.
    If you may any questions please contact us: