Dragon Drop - A Visual Basic Software Consultancy

Word Code / Best Practices

How To Create A Simple Template With A User Form

Step Eight

Now, the user will want the user form, or a dialog box, to appear when a new document is created based upon this template. When a new document is created a Document_New() event is raised. All we have to do is to trap the event via VBA and to fire off the dialog box.

The Document_New() event is handled within the 'ThisDocument' class. Double-click on 'ThisDocument' in the project tree and then when the text editor pops up in the right two drop down list boxes can be seen at the top right hand side.

The left hand lists all the objects associated with this class; if one is selected in the left-hand drop-down list box then the right-hand drop-down lists all the events associated with the object class.

So, we want to select the 'Document' in the left-hand drop down. The right hand will, by default, select the New event.

The following code ought to be visible in the editor window:

Note the first line; this is a Compiler Directive. 'Option Explicit' means that within this module all variables must be declared. If this line is not present then make sure that it gets typed in now. In addition go to the Tools | Options dialog box within the editor and make sure that the option 'Require Variable Declaration' is set. There is never a single reason why this option ought to remain cleared even though the default, incredibly, is to have it not set. Make sure that it is set and remains so forever.

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