Sample Code

Creating Word Templates

This sample code shows a few tips on how to create Word templates, commonly used for starting new documents. Methods for creating useful templates are not particularly well documented and some of the most useful techniques are hidden in obscure places.

Basic Steps To Creating A Template

This part of the process is well documented. To create a new template, open an existing document or create a new document that looks like how you want the template to look. Think about all of the places where you would want to replace information in the document that the template will produce. These are the places where you should put fields. Once you have created the document that you wish to use, save it as a template.

Word normally saves templates in its template directory. You can create new sub-directories in this directory and they will appear as one of the template folder tabs when Word opens a template as a result of creating a new document. Or, you can save the template in an existing sub-directory or the top-level template directory.

You can open existing templates and view the field codes (by moving the cursor to the field and right clicking, followed by choosing "Toggle Field Codes"). This can give you an idea about which fields to use in your templates.

Form Fields

For some templates, such as invoices, you might want to consider using form fields. To insert a form field into a template, either enable the forms toolbar or choose "Form Field" from the "Insert" menu.

Form fields are useful because the document created from the template may be protected from modification, except for the form fields. This means that the user can type into the form fields with gay abandon but never needs to worry about altering any other part of the document by accident.

Form fields can be named and then other fields can be calculated based on arithmetic operations on the named fields. For example, an invoice can have row and column totals, as in a spreadsheet, based on other values in the rows (e.g. rate x hours = total).

Replacable Fields

Other than form fields, fields of any kind can be inserted into the template wherever you'd like them to appear. One type of field that is particularly useful in a template is a replacable field that says something like "[Click here and type item]". This allows you to prompt the user of the template with what to type and then allows them to replace it.

The secret to this kind of field is to set the field value to "MACROBUTTON NoMacro [Click here and type item]". The text inside the square brackets will appear to the user when they use the template and will be replaced in its entirety when the field is selected.

For some reason, when this string is inserted into a field by the regular insertion process, an extra blank is added at its end. I insert the field first and then select it by right clicking. This brings up a popup that has "Toggle Field Codes" on it. Toggling the field codes will show you the actual string associated with the field and let you edit the space out.

Saving a Template Preview

Once you've created a template, you'll want to set the template preview so that a user can see what the template looks like, when they are selecting it from Word. To set the template preview, do the following:

  1. Open your custom template.
  2. On the "Edit" menu, click "Select All". Or, select the portion of the template that you want to appear in the preview.
  3. On the "Insert" menu, point to "AutoText" and then click on "AutoText" in the popup menu.
  4. In the AutoText list, select "Gallery Example" and then click "Delete". This will get rid of any preexisting template preview.
  5. In the "Enter AutoText entries here:" box, type "Gallery Example" and then click "Add".
  6. Ensure that the preview picture is saved with the template. To do this, select the "File" and then "Properties" menu. Click on the "Summary" tab and then check the "Save preview picture" box. Click the "OK" button.
  7. Save and close your custom template.