Dragon Drop - A Visual Basic Software Consultancy

Word Code / Best Practices

How To Create A Simple Template With A User Form

Step Three

Add bookmarks at the various points within template where the user is to insert data. Note that the bookmark names have sensible names. One of the golden rules of programming is to make sure that the object and variable names reflect what the variable and object does. One of the conventions of programming in VB or VBA is to prefix the variable or object name with a small lower case string to help remind the developer what the variable or object does. Since we are naming bookmarks we shall prefix 'bmk' to the bookmark names. In this example five bookmarks are placed onto the document; these are named 'bmkTo', 'bmkFrom', 'bmkDate', 'bmkSubject' and 'bmkStartHere'.

If one looks at the code and sees a reference to bookmark called 'bmkSubject' in a memo template is is obvious to the viewer that this bookmark handles the subject title of the document. Compare and contrast with the advice on the MVP site which says "Create two bookmarks in your template named “Text1” and “Text2”."

With the recommendation on the MVP site there is no way to know what 'Text1' and 'Text2' refer to. Are these bookmarks or text box items on a user form? More to the point; what sort of data will they contain? The MVP recommendation adds nothing but confusion and, ultimately, badly written code which is going to be murder to debug or maintain in the future. Therefore, always make sure that variables and objects have meaningful names and prefix them with an indicator as to what type it is.

Back to the template layout place the five bookmarks in place; 'bmkTo' goes to where the recipient's name shall go, 'bmkFrom' next to the sender's name, 'bmkDate' is adjacent to the Date text. Then the bmkSubject bookmark goes centrally on the page where the title of the memo will be and then, finally, 'bmkStartHere' is placed where the user will start typing.

One may realise that adding the bookmarks to the correct place within the document is a doddle. Why? Because the name of the bookmark gives a large hint as to where the bookmark is placed. If life is made easier for the programmer for herself then there is a significant less chance of her making coding errors.

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