Microsoft Office 2000/Visual Basic Programmer's Guide   

Custom Document Templates

One way to build a solution in Word is to create a custom template on which a user bases new documents. The template that's attached to a document is specified in the Document template box in the Templates and Add-ins dialog box (Tools menu). A document can have only one document template. Even when a document template is attached to a document, however, the Normal template remains loaded.

You should create a custom document template when:

Custom document templates are good for ensuring that all users have a consistent set of styles and tools for working on a particular project. For example, if your team is writing a book, you can create a document template that the writers use as the basis for each chapter.

Creating a Custom Document Template

To create a custom document template, click New on the File menu, click the General tab, click Blank Document, and then click Template under Create New.

You can write code in the template's VBA project. You can also add text, styles, and custom toolbars to the template. By default, the template is saved with the .dot extension, in the C:\Windows\Application Data\Microsoft\Templates folder; if user profiles are being used, the default folder is C:\Windows\Profiles\UserName\Application Data\Microsoft\Templates.

Note   By default, custom command bars are saved in Normal.dot. To save a command bar with a custom document template, create the command bar by clicking Customize on the Tools menu, clicking the Toolbars tab, and then clicking New. In the New Toolbar dialog box, click the document template's name in the Make toolbar available to list.

Creating a New Document Based on a Word Template

To create a new document based on your custom template, click New on the File menu to open the New dialog box. Your template should appear on the General tab, or on one of the other tabs if you saved it in a subfolder of the Templates folder. Click the template and then click OK.

You can also attach a custom template to an existing document. Doing so won't add any text that's in the template to your document, but any code, styles, and toolbars in the template will be available to your document. On the Tools menu, click Templates and Add-ins, and then click Attach to find and attach your document template.

If you look at the VBA project for a document that has a custom document template attached, you'll see that three projects appear in the Project Explorer in the Visual Basic Editor: the document's project, the custom template's project, and the Normal template's project. You can write code in any of these projects. In addition, you can call a procedure in the Normal template or in the custom template from a procedure in the document's project.

Note   When you create a document based on a template, that template appears in the document's References folder in the Project Explorer. If you open the References dialog box by clicking References on the Tools menu, you'll see that the template appears checked in the list of available references. Attaching a template to a Word document sets a reference to the template's VBA project, making the code that's in that template available to any procedure in the document.

Word includes a number of document templates that you can look at for tips on creating your own. If you've installed them, you can find these templates in the C:\Program Files\Microsoft Office\Templates\1033 folder of your Office 2000 installation.

Note   The 1033 subfolder beneath the Templates folder indicates the language ID for U.S. English language support. The language ID folder below C:\Program Files\Microsoft Office\Templates differs for each language. Templates you create yourself should be stored in the C:\Windows\Application Data\Microsoft\Templates folder or in a user profile-specific location. For more information about template deployment, see Chapter 2, "Designing and Deploying Office Solutions."