Dragon Drop - A Visual Basic Software Consultancy

Word Code / Best Practices

How To Create A Simple Template With A User Form

Step Ten

Make sure the frmMemo is diplayed in the editor window. Then click on the 'Show Code' button at the top left of the project tree.

We are going to write the code for the event handler for the command buttons. As with most command buttons the most important event is the Click() event. This is the event which we are going to trap.

In the left hand drop down list box select the cmdCancel object and the right hand drop down ought to display the Click event. This is because the Click event is deemed by Microsoft to be the default event for the command button class. If, however, one wishes to select another event (say the MouseOver event then one would search the drop-down list box for the required event).

The editor would automatically create a event handler procedure:

Option Explicit

Private Sub cmdCancel_Click()

End Sub

Again, if the first line is not Option Explicit then type it in and then check the Options panel. Note that there is never a valid reason to have this compiler directive removed.

The routine, cmdCancel_Click(), always runs when a user clicks on the Cancel command button when the form is active. Also, because through the Properties of this control the Escape keypress has been tied to this event so if ever the <Esc> is pressed then this event handler runs.

When the Cancel button is pressed the form has to close. Now, the calling procedure in the Document_New() event already has preset the form's Tag property to 'Cancel' and so there is no need to change anything other than to hide the form from view. Therefore, make the following change to the code (highlighted):

Option Explicit

Private Sub cmdCancel_Click()
  Me.Hide
End Sub

All of this is rather straightforward. When the button is clicked then the form hides. Note the object called 'Me'. This is a special pointer which points to the current open and active form. So, what this is saying is that the Hide method of the current form object is to be executed. And, like the name suggests, the form hides from view.

Now, please note here that even though the form object is hidden it still exists. It is present in all its aspects save one; it is invisble.

This is an important point to consider. When a form in VBA is shown (e.g. oForm.Show) then the form is what is known as Modal. This means that the form will appear and the user cannot do anything with the rest of the application, in this case Word, until the form disappears. Hiding the form counts as disappearing and then the user can work more on the form; but more importantly the control is returned back to the calling routine.

The opposite of a Modal form is a Modeless form. Modeless forms appear and work can continue on the application whilst the form is active. In graphical applications, for example, dialog boxes pop up here and there and contain palette and brush options; these are nothing more than Modeless forms and these are created by: oForm.Show vbModeless .

In our example we are dealing with a Modal form which means that we won't have to deal with the headaches that the Modeless forms invariably give the programmer. Modeless forms only are possible in Word 2000 and upwards. Also note that in VB the default is that forms are modeless; quite the opposite to VBA...

Going back to the template; the event handler for the OK button (cmdOK) will be:

Private Sub cmdOK_Click()

  Me.Tag = "OK"
  Me.Hide

End Sub

Much the same as the event handler for the cmdCancel button except that before the form hides the form's Tag's value is set to 'OK'. This is what will be interrogated in the calling routine in the the next step.

The code for the user form is now completed. The rest of the code will be back in the ThisDocument object of the template.

<< Back Next >>

Updates or Comments

If there are any suggestions for updates or comments then please drop us a mail at malcolm.smith@dragondrop.com.