Dragon Drop - A Visual Basic Software Consultancy

Word Code / Best Practices

How To Create A Simple Template With A User Form

Step Eleven

The code is now completed within the form class and we need to visit the calling routine which is in the ThisDocument class. As a reminder, the code currently looks like this:

Option Explicit

Private Sub Document_New()

  Dim oForm As frmMemo
  
  
  Set oForm = New frmMemo
  oForm.Tag = "Cancel"
  oForm.txtDate.Text = Format$(Date, "d mmmm yyyy")
  oForm.Show
  
End Sub

The situation, to recap, with the above code is that the form object, oForm, is created (the proper term in object orientated programming is 'instantiated'), one of the controls in the form is prepopulated and then the form object is shown.

What happen then is that the user will the dialog box displayed on the screen; she wil fill in the text boxes as appropriate, perhaps changing the date if required, and then click either the 'OK' or 'Cancel' button.

We now have to handle this response and if the 'OK' button was clicked then the data will go into the document at the correct positions.

So, the first thing we have to do is to work out if the user clicked on the 'OK' button. Now, if you recall we have placed the code under the cmdOK_Click() event which sets the form's Tag property to 'OK'. So, what we will do is to test for this value. If the Tag isn't set to 'OK' then the form was cleared by another way which means that the user wanted to stop processing the document.

We add the code (highlighted) below:

Private Sub Document_New()

  Dim oForm As frmMemo
  
  
  Set oForm = New frmMemo
  oForm.Tag = "Cancel"
  oForm.txtDate.Text = Format$(Date, "d mmmm yyyy")
  oForm.Show
	   
  If oForm.Tag = "OK" Then
    ActiveDocument.Bookmarks("bmkTo").Range.Text = oForm.txtTo.Text
    ActiveDocument.Bookmarks("bmkFrom").Range.Text = oForm.txtFrom.Text
    ActiveDocument.Bookmarks("bmkSubject").Range.Text = oForm.txtSubject.Text
    ActiveDocument.Bookmarks("bmkDate").Range.Text = oForm.txtDate.Text
    
    ActiveDocument.Bookmarks("bmkStartHere").Range.Select
    
    Unload oForm
    Set oForm = Nothing
  Else
    Unload oForm
    Set oForm = Nothing
    ActiveDocument.Close wdDoNotSaveChanges
  End If 
	
End Sub

It's quite obvious to see that the whole thing swings on the If statement. If the Tag property of the form is 'OK' then place the text from the controls to the range of the bookmark in the document.

Note the the code doesn't do the dreadful shortcut in this manner:

ActiveDocument.Bookmarks("bmkTo").Range.Text = oForm.txtTo

Whilst this will work this is not good programming practice. What the above example says is that the control itself is copied into the text of the bookmark range. Clearly this is not possible, nor is it desired. Okay, perhaps we know that the default property of the text box control is the Text property and this would get copied over. But, this is not good programming practice and one that should be avoided at all times.

If one wishes to copy or reference a control then just naming the control like this would be fine. But if one wants the Text property from the control then, please, tell the compiler that this is what you want. It makes for better programming and when one comes back to the code in a year's time then a properly written routine will make a lot more sense than one that doesn't.

Okay, sermon over. The last of these bookmark statements is to select the range of the "bmkStartHere" bookmark. In other words; the cursor goes to this point in the document ready for the user to start typing in the correct place.

The next line, "Unload oForm", simply removes the form object from memory. We have finished with it; we need it no longer so we'll get rid of it. This instruction simply tells VBA to remove the thing from memory so that the memory can be used once more.

Then there is the last line; the Set command. Remember how we used the Set command before to set a pointer (called oForm) to point to memory where the object lay? Well, the pointer is still pointing to where the object used to be. So for completeness sake we get the pointer to clear itself so that it's pointing to Nothing. If we were using the language C we would set it to Null.

Onto the other branch of the If statement; this is what happens if the user clicks on the Cancel button. Well, for a start none of the data from the dialog box will go onto the document. In the same way as in the 'OK' branch of the statement the dialog box is disposed of properly. But there is one further statement; the document itself is closed and all the updates are discarded.

Now, then at first glance this template looks complete. But there is one small problem which could bite the user. We will return to this in the next step but for the time being this template can be run rather successfully if either of the 'OK' or 'Cancel' buttons are clicked.

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