5.1 Document templates

Document templates

Word s templates offer a way to provide standard formatting and styles for documents. Like a form class in Visual FoxPro, a template lets you specify common elements like the page setup, headers and footers, and even boilerplate text so that every new document you create contains the same things. But in other ways, templates are more like class libraries because they contain styles that can be used in the document.

Every document you create in Word is based on a template, a file with an extension of DOT. If you don t specify which template to use, the Normal template is used. By default, Normal is an empty document containing a standard set of styles. It s installed when Word is installed, along with a number of other templates. To see the available templates, choose File|New from the menu. The New dialog (shown in Figure 1) appears. The Normal template is shown as "Blank Document."

Figure 1. Available templates. Choosing File|New from Word s menu lets you choose from among all the stored templates.

What goes into a template?

A template can contain anything that s in a document, from the simple blank document of the Normal template to a complex legal document, complete except for the names of the parties involved. Most templates fall somewhere in between, containing some text and formatting and some custom styles.

Templates can also contain macros, including one that runs automatically when you create a document based on the template. Templates can also include bookmarks that can be filled in, either by prompting the user or by Automation. In short, templates can be as much or as little as you choose to make of them.

Finding templates

Templates are stored in several different places. The templates (and wizards) installed by Word are put into a Templates subdirectory of the Office installation. In Word 2000, they then go down one level into a subdirectory named with the numeric code for the language you re using (1033 for American English). In Word 97, there s a set of subdirectories under Templates that contains the various supplied templates and wizards. (In both cases, wizards have a WIZ extension.)

In addition, each user can set a user template directory and a workgroup template directory. The user template directory is for templates specific to the individual, while the workgroup template directory is meant for templates shared by the user s entire team. The default location for user templates varies according to the version of Word and Windows, but it isn t always in the Office directory tree. In some cases, templates are stored as part of the user s profiles in the Windows directory tree. No matter where the user template directory is, the templates in that directory appear on the General page of the New dialog. Templates in subdirectories of that directory are placed on additional pages of the dialog the pages are labeled with the subdirectory names. Creating new subdirectories creates additional pages, as you can see in Figure 1.

The Workgroup templates setting is empty by default. When it s set, the same two-level structure applies. Templates in the directory itself appear on the General page, while subdirectories have their own pages in the New dialog.

Both can be set on the File Locations page of Word s Tools|Options dialog (see Figure 2).

From the Automation point of view, you can determine (or specify) the user and workgroup template locations by checking the DefaultFilePath property of the Options object (which is a member of the Word Application object). Pass wdUserTemplatesPath (2) to find the user s own templates and wdWorkgroupTemplatesPath (3) for the workgroup s templates. (The NewDocument method of the cusWord class in the Developer Download files available at www.hentzenwerke.com accepts a template with or without a path and attempts to locate the template file before creating a new document. The cusWord class is discussed in Chapter 15, "Wrapping Up the Servers.")

Figure 2. Pointing to templates. Word s Options dialog lets you indicate where user and workgroup templates are stored.

Using templates

You can use templates in several ways when automating Word. The simplest is to create new documents based on existing templates. To do so, specify a template, including the path, as the first parameter of the Documents.Add method. For example, to create a new document based on the agenda template shown in Figure 1, you would use the following code. (Unlike other examples in this book, you can t work along with this one because you don t have agenda.DOT on your machine. We ll return to this example later with one you can actually try.)

#DEFINE wdUserTemplatesPath 2

cTemplatePath = oWord.Options.DefaultFilePath( wdUserTemplatesPath )

* AddBS() adds a trailing backslash - before VFP 6, you need FoxTools loaded.

oDocument = oWord.Documents.Add( AddBS(cTemplatePath) + "agenda.DOT" )

Once you create a new document based on a template, you can treat that document just like any other new document. However, you have the advantage that it contains whatever special text, formatting, and styles were stored in the template.

Creating templates

You can also create templates with Automation. Any document can be saved as a template by passing the appropriate parameter to the SaveAs method. To create a new template, create a document, format it as desired, create any styles you want the template to have, then call SaveAs, like this:

#DEFINE wdFormatTemplate 1

#DEFINE wdUserTemplatesPath 2

cTemplatePath = oWord.Options.DefaultFilePath( wdUserTemplatesPath )

oDocument.SaveAs( AddBS(cTemplatePath) + "MyNewTemplate.DOT", wdFormatTemplate)

Now that we ve created a custom template, we can create a new document based on it. Here s the example from the previous section, but using the new template rather than agenda.DOT:

#DEFINE wdUserTemplatesPath 2

cTemplatePath = oWord.Options.DefaultFilePath( wdUserTemplatesPath )

* AddBS() adds a trailing backslash - before VFP 6, you need FoxTools loaded.

oDocument = oWord.Documents.Add( AddBS(cTemplatePath) + "MyNewTemplate.DOT" )

As in interactive Word, you can store the template in a subdirectory to have it appear on a different page in the File|New dialog. Of course, if you re working with it through Automation, you don t really care where it appears. In fact, with Automation, it doesn t matter where you store templates because you can specify where Word should look for them. However, keeping them together with other templates means that interactive users can find them, as well. On the other hand, for an automated process, you may prefer to keep your templates well hidden so that users can t find them and delete them or use them in ignorance.

You can also specify that new documents you create are intended to be templates. Pass .T. as the second parameter to the Documents collection s Add method to indicate that the new document is a template rather than a regular document. Then, when you save it, it gets a DOT extension instead of DOC and the default location for it is the user templates directory. In that case, you don t need to pass the second parameter to SaveAs.

Putting templates to work

We both use templates extensively in our work to provide a uniform appearance for documents and to save time and effort. We d no more use Word without templates than we would use VFP without classes. Especially when combined with the ability to define custom styles, templates provide a way to streamline document creation.

 

Copyright 2000 by Tamar E. Granor and Della Martin All Rights Reserved



Microsoft Office Automation with Visual FoxPro
Microsoft Office Automation with Visual FoxPro
ISBN: 0965509303
EAN: 2147483647
Year: 2000
Pages: 128

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net