Working with Microsoft Word Templates—The Foundation

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

 

Charles Kyle Kenyon
Kenyon Law Offices
Madison, Wisconsin

Frank Rice
Microsoft Corporation

May 2003

Applies to:
   Microsoft® Word 2000 and later versions

Summary: Learn how templates can make developing your Word application much easier. Word templates allow you to control when and where features such as styles, macros, keyboard shortcuts, custom toolbars, and AutoText entries show up in your applications. Because templates allow access to all of these features, a large part of your development and document creation job will be done for you. (23 printed pages)

**Note   **For security information, see Security Notes for Microsoft Office Solution Developers.

Contents

Introduction
What You Will Learn
Creating Templates
About Template Folders
Global Templates
The Normal.dot Template
The Hierarchy of Templates
Templates Styles
Organizing Templates
Working with Templates
Using the Organizer
Macros in Templates and Documents
Troubleshooting Templates
Conclusion

Introduction

Templates are a special type of Microsoft® Word document that can hold text, styles, macros, keyboard shortcuts, custom toolbars and AutoText entries. A document created using a template will have access to all of these features and so a large part of your development and document creation job will be done for you (assuming your templates are well thought out). But you don't need to use all of these features in order for templates to help you and, more importantly, to help your customers.

What You Will Learn

After working with the material in this article, you will be able to:

  • Distinguish between a template and an ordinary document.
  • Distinguish between a document template and a global template.
  • Save a document as a template.
  • Attach a different template to your document.
  • Open a template for editing.
  • Find the location of templates on your computer.
  • Make additional tabs under the New option on the File menu (or remove unwanted tabs).
  • Set up and use Workgroup Templates.
  • Create a Global Template.
  • Copy macros, styles, and AutoText entries from one template (or document) to another.
  • Use Visual Basic for Applications (macros) to automate your work.

Creating Templates

As we stated earlier, templates are a special type of Word document. They hold components that can be used by other documents, such as, text, AutoText, macros, and toolbars. In addition, they hold style definitions as well as information on any modifications to your user interface such as keyboard shortcuts and changes to the built-in menus and toolbars.

Note   AutoText provides a storage location for text or graphics you might want to use over and over, such as for standard contract boilerplates or long distribution lists. Each selection of text or graphics is recorded as an AutoText entry and is assigned a unique name.

A template and a document have a different internal file structure. Over the years, the distinction has become less and less, but the more advanced your use of Word becomes, the more important it is to make the distinction. When you save a document as a template, Word will attach the extension of ".dot" to the end of the name instead of ".doc". However, it is not the extension that makes it a template. Merely changing the name either way will not change a document into a template or a template into a document (although doing so may confuse you and other users). Microsoft Windows® may think that such a misnamed file is a document, or template (depending on the extension), Word knows the difference. You can make a document into a template from within Word using Save As under the File menu. You can't directly change a template into a document. You can, however, create a new document based upon a template and then save that new document as a document. The following VBA code statement is used to create a new document based on a custom template:

...
Document.Add "C:\Templates\MyTemplate.dot
...

This statement works as long as the template is located at C:\Templates. Otherwise, an error message is displayed. The Options object which is a property of the Application object has a number of properties and methods including the DefaultFilePath property which is useful for determining the path for files. To make the previous statement a little more flexible, we could rewrite the statement as:

...
Dim strUserTemplates As String

strUserTemplates = Options.DefaultFilePath(wdUserTemplatePath)

Documents.Add strUserTemplates & "\MyTemplate.dot"
...

You can also use the AttachedTemplate property of the ActiveDocument to attach the template to the active document. This statement attaches the template "Letter.dot" to the active document:

...
ActiveDocument.AttachedTemplate = "C:\Templates\Letter.dot"
...

Much more information on creating a template can be found in the articles Creating a Template–The Basics (Part 1) by MVP Suzanne S. Barnhill and Creating a Template (Part II) by MVP John McGhie.

Although this article touches on the basics of using templates, the intent is to give you information about templates that may not be covered in other articles.

If you are creating a document template, I urge you to pay close attention to Styles.

Note   A style is a set of formatting characteristics that you can apply to text, tables, and lists in your document to quickly change their appearance. When you apply a style, you apply a whole group of formats in one simple task.

In constructing or editing a template, you should always change document formatting by using the Styles and Formatting option on the Format menu. This makes it much easier for users to access the styles as opposed to trying to modify styles directly in a template.

Template formatting done directly (outside of styles) can be confusing for the user and may needlessly make life more difficult. If you are the user of your own templates, then it's not too late to develop the habit of formatting using styles.

Both Word and Windows like to hide things from you, feeling that too much information tends to confuse. You may agree with this philosophy or not. However, this article is written expecting that you can see some of this hidden information. Specifically:

  • Filename Extensions

    If you are in a Windows Explorer window that has Word documents in it, do the names show the three-letter extension ".doc?" If not, to see these, you need to:

    1. On the Tools menu, click Folder Options, and then click the Views tab.

    2. Clear the selection that says something similar to Hide filename extensions for known file types and click OK (or Close).

      If you are in the Mac OS X Finder, you need to be in As List or As Columns (both options located on the View menu) to see the extensions. If they are not visible, click Finder on the Preferences menu, and select Always show file extensions.

  • Paragraph marks and section marks

    These non-printing characters are at the heart of Word formatting. If you can't see them, it is very difficult to work on a document and the chances of damaging the formatting and thus corrupting the document becomes very high. The default is to not show these characters. When you are working on formatting a document, you need to see them. Click the Show/Hide toolbar button (on the Standard Toolbar, to the left of the Zoom drop-down list). You can also set this by clicking Options on the Tools menu, clicking the View tab, and then selecting All under Formatting Marks.

    Aa140287.odc_wdtemp01(en-us,office.10).gif

    Figure 1. The Show/Hide toolbar button

    Note that you can change these back later.

    Most advanced computer users leave these displayed to save time because not seeing these can make it more difficult to move around in the document.

Creating a Document from a Document Template

When you select New on the File menu, you open the New Document task pane.

Aa140287.odc_wdtemp02(en-us,office.10).gif

Figure 2. The New Document task pane

There are four sections: the one we want is labeled New from template. Click the General Templates link to see the selection available on your computer.

Aa140287.odc_wdtemp03(en-us,office.10).gif

Figure 3. The Templates dialog box

When you pick a template and then create a new document based on that template, the template remains "attached" to the document. Any text that is in the template will be available in your new document.

Any template styles used in the document (whether in the template's text or in text that you type or insert) become defined in the document and will stay with the document even if the attachment is later broken. If the template's style definition is changed after it is used in the document, the document style will not change unless the template is reattached or the style is otherwise copied into the document again. (More on attaching a template to an existing document shortly)

AutoText entries, macros, keyboard customizations, and toolbars in the template are available to the document so long as the document remains attached to the template, but are not normally transferred into the document. (Documents cannot hold AutoText entries but can have keyboard customizations, macros, and toolbars.)

Attaching a Template Programmatically

In addition to being able to attach a template from the user interface, you can also do so from code as in the following sample. This sample creates a recordset from the Northwind sample database, attaches a template to the current document, and then populates a series of bookmarks in the document:

Private Sub Document_Open()
   Dim Conn As New ADODB.Connection
   Dim rsInvoices As New Recordset
   Dim objDoc As Document
   Dim objTable As Table
   Dim strUserTemplates As String
   Dim strConnection As String
   Dim strCustomer As String
   Dim strDBPath As String
   Dim strSQL As String
   Dim intNumRows As Integer
   Dim i As Integer
   
   On Error Resume Next
   
   strDBPath = "C:\Program Files\Microsoft OfficeXP" & _
      "\Office10\Samples\Northwind.mdb"
   
   ' Open a connection to the Northwind database.
   strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Persist Security Info=False;" & _
      "Data Source=" & strDBPath
   
   Conn.Open strConnection
   
   strSQL = "SELECT * FROM Invoices " & _
      "WHERE (((Orders.ShipName)='QUICK-Stop'))"
   
   rsInvoices.Open strSQL, Conn, adOpenStatic, adLockReadOnly
  
   strUserTemplates = Options.DefaultFilePath(wdUserTemplatesPath)
   
   Set objDoc = Documents.Add(strUserTemplates & _
       "\Simple Invoice.dot")
   
   ' Retrieve and set company information.
   SetBookmark ActiveDocument, "CoName", _
      rsInvoices("Customers.CompanyName").Value
   SetBookmark ActiveDocument, "CoAddress", _
      rsInvoices("Address").Value
   SetBookmark ActiveDocument, "CoCity", _
      rsInvoices("City").Value
      
   ' To handle Nulls in this field.
   If IsNull(rsInvoices("Region").Value) Then
      SetBookmark ActiveDocument, "CoState", _
         ""
   Else
      SetBookmark ActiveDocument, "CoState", _
         rsInvoices("Region").Value
   End If
   SetBookmark ActiveDocument, "CoZip", _
      rsInvoices("PostalCode").Value
      
   ' Retrieve and set Bill To information.
   SetBookmark ActiveDocument, "BillToName", _
      rsInvoices("Salesperson").Value
   SetBookmark ActiveDocument, "BillToCompany", _
      rsInvoices("ShipName").Value
   SetBookmark ActiveDocument, "BillToAddress", _
      rsInvoices("ShipAddress").Value
   SetBookmark ActiveDocument, "BillToCity", _
      rsInvoices("ShipCity").Value
   
   ' To handle Nulls in this field
   If IsNull(rsInvoices("ShipRegion").Value) Then
   SetBookmark ActiveDocument, "BillToState", _
      ""
   Else
   SetBookmark ActiveDocument, "BillToState", _
      rsInvoices("ShipRegion").Value
   End If
   SetBookmark ActiveDocument, "BillToZip", _
      rsInvoices("ShipPostalCode").Value
      
   
   Set objTable = ActiveDocument.Tables(3)
   
   i = 1
   
   objTable.Cell(i, 1).Range.Text = "Description"
   objTable.Cell(i, 2).Range.Text = "Amount"
   
   Do While Not rsInvoices.EOF
      
      i = i + 1
      
      objTable.Cell(i, 1).Range.Text = rsInvoices("ProductName")
      objTable.Cell(i, 2).Range.Text = Format(rsInvoices("ExtendedPrice"), "Currency")
      
      rsInvoices.MoveNext
   Loop
‘ Clean up.
Set objDoc = Nothing
Set objTable - Nothing
          
End Sub

Public Sub SetBookmark(objDoc As Document, sBookmark As String, sValue As String)

    If objDoc.Bookmarks.Exists(sBookmark) Then
    
        objDoc.Bookmarks(sBookmark).Range.Text = sValue
    
    End If

End Sub

The first section of the subroutine is used to open up a connection to the Northwind sample database.

...
strDBPath = "C:\Program Files\Microsoft OfficeXP" & _
      "\Office10\Samples\Northwind.mdb"
   
   ' Open a connection to the Northwind database.
   strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Persist Security Info=False;" & _
      "Data Source=" & strDBPath
.......Conn.Open strConnection
...

Next, we use a SQL statement to open a Recordset object based on the Invoices query in Northwind. In the sample, I wanted to narrow down the number of records retrieved so the SQL statement uses a WHERE clause to retrieve records for the Quick-Stop shipper.

...
strSQL = "SELECT * FROM Invoices " & _
   "WHERE (((Orders.ShipName)='QUICK-Stop'))"
rsInvoices.Open strSQL, Conn, adOpenStatic, adLockReadOnly
...

Then, we attach the Invoice.dot template to the document by using the wdUserTemplatesPath enumeration of the DefaultFilePath property of the Options object:

   strUserTemplates = Options.DefaultFilePath(wdUserTemplatesPath)
   
   Set objDoc = Documents.Add(strUserTemplates & _
       "\Simple Invoice.dot")

Next, we start parsing the individual fields of the record set into the bookmarks that exist in the template:

   ' Retrieve and set company information.
   SetBookmark ActiveDocument, "CoName", _
      rsInvoices("Customers.CompanyName").Value
   SetBookmark ActiveDocument, "CoAddress", _
      rsInvoices("Address").Value
   SetBookmark ActiveDocument, "CoCity", _
      rsInvoices("City").Value
      
   ' To handle Nulls in this field.
   If IsNull(rsInvoices("Region").Value) Then
      SetBookmark ActiveDocument, "CoState", _
         ""
   Else
      SetBookmark ActiveDocument, "CoState", _
         rsInvoices("Region").Value
   End If
   SetBookmark ActiveDocument, "CoZip", _
      rsInvoices("PostalCode").Value

The code does this by calling the SetBookMark function which uses the Range object of the Bookmarks collection:

Public Sub SetBookmark(objDoc As Document, sBookmark As String, sValue As String)
   If objDoc.Bookmarks.Exists(sBookmark) Then
   
      objDoc.Bookmarks(sBookmark).Range.Text = sValue
    
   End If
End Sub

After the bookmarks have been set, we next create a reference to one of the three tables that are located in the template. In this case, the third table contains the product information including the name of the product and the total sales for that product:

   Set objTable = ActiveDocument.Tables(3)
   
   i = 1
   
   objTable.Cell(i, 1).Range.Text = "Description"
   objTable.Cell(i, 2).Range.Text = "Amount"
   
   Do While Not rsInvoices.EOF
      
      i = i + 1
      
      objTable.Cell(i, 1).Range.Text = rsInvoices("ProductName")
      objTable.Cell(i, 2).Range.Text = Format(rsInvoices("ExtendedPrice"), "Currency")
      
      rsInvoices.MoveNext
   Loop

After creating a Table object, we set the column headings and then loop through each row in the set of records to populate the table. We use the Format method to format the second column as type Currency.

The end result is an invoice in Word populated by filtered records from an Access database:

Aa140287.odc_wdtemp04(en-us,office.10).gif

Figure 4. Invoice created from a template

About Template Folders

The templates that appear in the New Document task pane are those in your User templates and WorkGroup templates folders. Various design changes over the years have resulted in a most peculiar method of operation for templates. Essentially, the "On my computer" link is the main source of templates. You will work almost always with templates you have created and stored in your template folders.

User Templates Folders

When you click the "On my computer" link, Word examines the User templates folder location (which is set by clicking Options on the Tools menu, clicking the File Locations tab and then setting the User templates path). If you ask Word to save a document as a template, this is the folder that is used for that purpose.

Aa140287.odc_wdtemp05(en-us,office.10).gif

Figure 5. The File Locations tab on the Options dialog box

Any templates that are in the top level of this folder appear in the General tab. Any sub-folders of this folder are examined to see if they contain templates. If they do, each folder appears as a tab in this dialog. Blank folders, and folders that do not contain templates, do not appear.

In a somewhat confusing design decision, various tabs appear that are NOT folders at this location. And in them appear templates that are not physically located in the folder. These are the "built-in" templates. A built-in template may not exist as a file. Word contains some templates that are hard-coded into the program; they exist as physical files only if you make a change to them.

To remove built-in templates, you must use the Control Panel. Specifically, click Start, (Windows 2000, point to Settings), and then double-click the Control Panel. Double-click Add/Remove Programs, scroll to Microsoft Office and then click Change. Clear the folders of templates that you do not wish to see, and then click Update.

Word insists that you save to the User templates folder whenever you save a template. You can then move the templates any place you want to, but if you want them to show up in the New dialog box (available from the File menu), they must be in either the User templates folder or the Workgroup templates folder.

To see which template is attached to a document, click Templates and Add-ins on the Tools menu. The full path to the attached template will appear in the Document Template path. If the entry there simply shows "Normal", the document has no template attached.

Workgroup Templates Folders

Typically in a corporate installation of Word, there will also be a place on the network where you keep the templates you want other people to use. You need to set this location for each user. You can do this by clicking Options on the Tools menu, clicking the File Locations tab, and then clicking the Workgroup templates entry. Click Modify, and browse to the network location where your company keeps its templates.

When you update this location, the templates and subfolders that contain templates will all appear in the New Document task pane. Regrettably, they appear all mixed in together. For this reason, it is a good practice to store ALL your corporate templates in sub-folders of the Workgroup templates location. This way they will appear under appropriate tabs on the dialog. After making this change, run a user update .MSI to remove the built-in templates from the dialog. As a rule of thumb, it is best to keep the number of tabs at around five to seven so as to not to provide too many choices.

Note   You can find extensive information about the Windows Installer and creating installation packages with the Windows Installer SDK which can be downloaded from the Microsoft Developer Network (MSDN).

The Workgroup templates folder must be a different folder than the User templates folder even if both are stored on the same computer. If the Workgroup templates folder is on the same computer as the User templates folder, it should be in the folder that holds the templates folder (in other words, the parent to the User templates folder), not in the User templates folder. The parent folder is typically the Microsoft Office folder.

The locations for the User templates and Workgroup templates folders are set initially by the Office Setup program (possibly using network administration policies). You can change the location of these folders anytime by using the procedure described earlier. Other Office applications may also store templates and other files in these folders so it is worthwhile to note that changing the default folder locations will change the templates folders locations for all Office programs, not just Word.

Global Templates

Global templates are a type of add-in for Word. Global templates are different from document templates; so different, in fact, that giving both the name "template" can be confusing. They are normally not "attached" to any document and usually do not contribute text or styles to any document. Instead they are excellent vehicles for holding and sharing AutoText, macros, and toolbars.

You can make any template global by clicking Templates and Add-Ins on the Tools menu, and then clicking Add. The Add Template dialog box will open showing the User templates folder's contents to choose from. You can navigate to another location and add a template from there. Since they don't contribute text and are not used to start new documents, global templates are probably best kept separate from both user template and workgroup template folders. If you add a template as an add-in this way, it will remain global until you restart Word. At that time, you could add it again, if needed. Or, you could make it load automatically on start up by putting the template or a shortcut to the template in the Word Startup folder. This is not the Startup programs folder in your Windows Start menu, but rather one specifically for Word. You can find (or change) its name and location with by clicking Options on the Tools menu, and then clicking the File Locations tab.

Sharing Global Templates over a Network

If a global template is to be shared over a network, it should be placed in a folder on the network server to which all users have file read access. Each user's network login file should be set to copy the file to the user's local startup drive when the user logs onto the network if the network version is newer than the user's version. That way you can update the template without everyone having to be off from Word when you do it.

If you can't work with login scripts or aren't worried about updating the template, you will probably want to use shortcuts to the template in each user's Word Startup folder. That way, any changes will automatically update everyone's instance of Word. If the template is your own and not shared, you can either put it in your own Startup folder or keep it elsewhere and use the shortcut to load it into Word.

Temporary Global Templates

You can also use a temporary global template which is not loaded at startup, as well. Such a template can share resources which are not used or needed in most of your documents but are used by multiple templates and their documents. To do this, you would include a VBA command in the AutoOpen and AutoNew macros of the templates that need those resources. These would load your global as an Add-In for that session of Word only. This way, when one of the documents needing your template's resources is created or opened, the resources will be available.

A good place to store such a global template might be in a folder in your Word Startup folder named GlobalTemplates. That way, it would not load at startup, would be easy to find, and would not show up in the New dialog box (on the File menu).

The Normal.dot Template

Normal.dot is a special global document template created and used by Word. It is a global template, but is often used as a document template. Unlike other global templates, Normal.dot must be in the User templates folder, and unlike other global templates, it should not be shared. Also unlike other global templates, it shares styles with all open documents (including other templates). When you click the NewDocument button or go to New option on the File menu and then click Blank Document, by default, you get a document based on the Normal.dot template.

Normal.dot is usually considered at least as personal as the locked bottom drawer of someone's desk. It contains custom styles and other tailored settings so people will usually be offended if you modify their Normal.dot. This includes putting VBA code in the Normal.dot and distributing it to others. There are a couple of major reasons against this. If Normal.dot becomes corrupted for some reason and has to be replaced, then any code in the template is also lost. Secondly, if the user had custom settings in their Normal.dot, all of those settings are lost when they use your version.

If Word is unable to find the Normal.dot file when started, it will create one, using its own default settings. The default installation location for Normal.dot is the User templates folder. (In some language editions, Normal.dot will have a slightly different name. Also, at least one virus renames Normal.dot.) Except in unusual circumstances (multiple users on one computer or multiple versions of Word) there should only be one copy of Normal.dot on a computer.

Note   That there is also a fourth kind of template that is not covered in this article, called a numbering list template. For some reason, Microsoft chose to use the term "template" for its numbering lists as well. These are registry entries and not separate files, unlike the templates addressed in this article.

The Hierarchy of Templates

Thus far, we have see how to attach document templates, global templates, and the Normal.dot template. What happens if there are conflicts (two AutoText entries or macros with the same name, for instance)? They defer to each other according to rules set by Word (though the rules aren't very easy to discover). You usually don't need to know this hierarchy unless you start using the same names for macros, styles or AutoText entries in multiple templates loaded simultaneously. The order is:

  • First, look in the document:
    • Any macros or styles in the document will be used in preference to others if they have the same name.
    • Any toolbar or keyboard modifications stored in the document will trump those stored elsewhere. (If the document and a template both have toolbars with the same name, though, they will both be available when the document is active.)
  • Next, check the attached template:
    • Any macros or styles in the attached template will take priority over any other except those of the same name in the document.
    • Any styles changed in the template after the document was created will be available as changed to the document by updating styles.
    • Any AutoText entries in the attached template will be used in place of those with the same name in Normal.dot or global templates. (Documents don't contain AutoText entries.)
    • Any toolbar or keyboard modifications stored in the attached template will trump those stored in Normal.dot or in other global templates.
  • Then, check Normal.dot:
    • All styles in Normal.dot are available to all documents. Those styles already in the document will not take on the attributes of styles in Normal.dot unless you update them. (Normal.dot has many more styles than are ever used in one document.)
    • All macros and AutoText in Normal.dot are available to all documents (unless preempted by an item of the same name in the attached template or the document). Normal.dot is not the place to store shared macros.
    • Any toolbar or keyboard modifications stored in Normal.dot are applied. In case of conflicts between Normal.dot and other globals, Normal.dot wins.
  • Then, check other global templates and add-ins: Again, these do not contribute styles to documents but all macros, toolbars and AutoText entries are available from a global template.
    • Styles in global templates are irrelevant to documents (unless the style is incorporated in an AutoText entry).
    • If there is a macro or AutoText entry with the same name in Normal.dot, the attached template, or the document, as the name in any other global template, the macro or AutoText entry in the global template will not be used (except for an AutoExec macro).
    • Any toolbar or keyboard modifications are applied unless they conflict with something higher in the hierarchy.
    • If there are multiple global templates, they are checked in the order they appear in the Templates and Add-Ins dialog box (with the first found taking priority over those appearing lower on the list).
  • Finally, check Word itself:
    • The Word application stores its styles, AutoText entries, formatted autocorrect entries and toolbar settings in Normal.dot but will recreate Normal.dot with default settings if it can't find the Normal.dot file when started.
    • While the Word application does not contain macros, as such, it does contain Word commands (which show up as a category in the Word macro list). These can be intercepted by macros which have the same name as the command.

Templates Styles

The reasons for using styles in a template are the same as those for using them in your documents:

  • Consistency—When you use styles to format your templates, documents having the same function will have a similar, familiar appearance, and will be easier for the reader to understand. Each section is formatted the same and therefore, provides a professional, clean-looking document.
  • Easier to Modify—If you use styles in your template consistently, you only need to update a given style once if you want to change the characteristics of all text formatted in that style.
  • Efficiency—You can create a style once, and then apply it to any section in the documents based upon the template without having to format each document individually. You can change a style in a template and update the styles in the attached documents easily.
  • Table of Contents—Styles can be used to generate a table of contents quickly.
  • Faster Navigation—Using styles lets you quickly move to different sections in a document using the Document Map feature and the vertical scrollbar's tips.
  • Working in Outline View—Styles allow you to outline and organize your document's main topics with ease.
  • Legal Outline Numbering—Numbering, when linked to styles, allows you to generate and update consistent outline numbering in legal documents, even ones with complicated numbering schemes like municipal law, tax law, and mergers and acquisitions documents. Failure to use numbering linked to styles is one of the easiest ways to really mess up a Word document. This applies to templates even more!
  • Efficiency of Word—Files which are predominantly manually formatted are less efficient than those which have formatting that has been imposed by styles: manually formatted files, such a converted documents which have been opened from the File menu, can be bloated in file size (bytes) and do not render to the screen efficiently when you scroll through them. This is because Word is a styles-based application: it first reads the attributes of the underlying style, then has to broadcast anything contrary (that is,.manually formatted on top of that). As such, a lengthy document that has been predominantly manually formatted, will behave sluggishly because Word has to work harder at managing it. Additionally, the print formatting processes are equally labored as opposed to using styles. (Each paragraph mark in Word will carry up to thirty different formatting commands for the screen and printer. These can all be replaced by one style setting.)
  • HTML AND XML—A fully structured, styled template will move into HTML and XML incredibly well.
  • Bottom line—Use of any direct formatting in a document template is a very bad idea. It will cause the users of your templates (and, if there is any justice in the world, you) headaches.

Note   In Word 2000 (and later), styles are listed in alphabetical order. In Word 97 styles listed in the drop-down list are not displayed in alphabetical order. Word 97 lists styles in the following order in the Style box list:

  • Heading styles
  • Normal style
  • User-defined styles in alphabetical order
  • Body Text styles
  • List styles
  • All other styles listed alphabetically.

Warning   I do not recommend selecting the Automatically update the document styles box (available by clicking Templates and Add-Ins on the Tools menu) especially in an environment where multiple users work on the same document. This feature will update the style each time you make a formatting change in a paragraph that has a style attached.

Organizing Templates

How to Add User-Defined Tabs

Aa140287.odc_wdtemp06(en-us,office.10).gif

Figure 6. The Publications tab on the New Document dialog box

When you want to save a document as a template, Word will take you to your user templates folder. If you store the template there, it is then available in the General tab for new files. The other tabs that you see under the New dialog box are folders in either the user templates folder or the workgroup templates folder. If you want to add a custom tab, you must first add a folder and then store a template there. (In Word 2000, the tab won't show up if there isn't a template in the folder.)

Word 97 stores the templates that come with it in these same folders. Word 2000 keeps its built-in templates elsewhere. If you want your template to show up under the Letters & Faxes tab, you need to create a folder with that title in your user Templates folder. Just click Save As on the File menu, and select template as your file type. Before you save the template, create a new folder called Letters & Faxes if one isn't there, and then open that folder and store your template there.

Create Tabs from a Workgroup Templates Folder

You can create organization folders in your workgroup templates folder as well and store your workgroup templates there. These tabs will then show up in the New dialog box for everyone who has set that folder as the location for workgroup templates.

If you give your folders (that you create in the user or workgroup templates folders) the same name as tabs already showing up under the New dialog box, your templates will show up under those tabs. You can have folders with the same names in your personal templates folder and have your workgroup templates folder to take advantage of this.

Aa140287.odc_wdtemp07(en-us,office.10).gif

Figure 7. Template folder hierarchy

Note that the folder depth allowed for templates folders is two levels: the Templates folder and one level of folders therein. The diagram above shows five levels. You can put subfolders in second-level folders but Word will ignore that structure and act as if you put all the templates directly in the folder at the second level.

Folder Structure Differences

Word 97/Word 98

If your templates folder is structured as in the diagram, when you display the New dialog (on the File menu), you will see four custom tabs and five custom templates in your dialog box. If you click on the AA tab, you will see no templates.

Word 2000/Word 2002/Word XP

If your templates folder is structured as in the diagram, when you display the New dialog, you will see three custom tabs and five custom templates. No tab is shown for AA because it contains no templates.

All

If you click on the tab AB, you will see templates 11, 12, and 13 as options for starting your new document. If you click on the tab AD you will not see any folders. You will see the following templates: 17, 18, 21, 31, 32, 33, 34, 35, 36. All templates that are anywhere within folder AD, including in subfolders, are displayed.

Other Tabs That Show Up in the New Dialog

  • The General tab (Word 97 and later)

    The General tab displays all templates in the user templates folder, the workgroup templates folder, and one that says Blank Document. You won't find a Blank Document.dot if you look in any of the templates folders, this is actually Normal.dot.

  • The More tab (Word 97 and later)

    If you have more folders in your user templates folder and in your workgroup templates folder than can fit on two rows of tabs in the New dialog box, the last tab on the second row will be More. Clicking on this will give you all of the folders in your templates folders.

  • Other tabs that are not Folders (Word 2000 and later)

    In Word 2000 (and later), the templates that come with Word show up under tabs in your New dialog box but if you look for them, you won't find them. That is "by design." Unless you have created a folder that has the same name as one of these tabs, there will be no folder with that name. If you do create such a folder (in either your user templates folder or your workgroup templates folder) you will have a folder that matches the tab. Any templates that you put into that folder will show up under the tab in the New dialog. (However, the templates under that tab that come with Word will still not be in the folder.)

Removing Tabs from the New Dialog Box

First, read the section above on How to Add User-Defined Tabs so that you have an understanding of how templates and the templates' folders are organized and work.

Word 97/98

Move the folder (let's call it Folder A) outside of the Templates folder (or workgroup templates folder). If you want the templates in Folder A available under a different tab, you can move them to the folder for that tab or your can simply put the folder you no longer want to appear as a tab into a different folder that still will appear as a tab. If you put the folder (Folder A) into a different folder (Folder B) in the templates folder, the folder you moved (Folder A) will no longer appear as a tab in the New dialog on the File menu but its templates will all appear under the tab for Folder B.

Word 2000 (and thereafter)

If your tab is created by a folder in your user templates folder or your workgroup templates folder, follow the instructions for Word 97/98. If it is a built-in folder, then you will have to uninstall those templates using Add/Remove Programs. For detailed instructions, see the Knowledge Base article WD2000: General Questions and Answers About the Location of Word 2000 Templates.

  1. Close Word.
  2. Open the Control Panel and then select your version of Microsoft Office in Add/Remove Programs.
  3. Click the Change button.
  4. Click Add or Remove Features and then click Next.
  5. Click the plus (+) symbol next to Microsoft Word for Windows to expand it.
  6. Click on the plus (+) symbol next to Wizards and Templates to expand it.
  7. You will see a number of categories such as Letters, Memos, etc. These categories correspond to the tabs in the New dialog box.
  8. Select the down arrow just to the left of any of these category names.
  9. Click the X Not Available option. This means that these templates will not be available unless you go back and reinstall them.
  10. Then click on the Update Now button at the bottom right of the dialog.
  11. If you just want to get rid of the tab but you still want some or all of the templates, you will need to reproduce those templates before you uninstall the category (tab). You can do this by creating and saving a new template. To do this, select the New Template checkbox in the New dialog box and save the template to a different folder/tab. Alternatively, the templates may already exist on your hard drive, just not in your user templates folder. Look for a folder labeled 1033 (English language version) in C:\Program Files\Microsoft Office\Templates\. Again, this is not your user templates folder. Almost all installed built-in templates are in this folder. You can copy the one you want into your user templates folder (or to a subfolder).

Working with Templates

Opening a Template for Editing

You will need to know the location of the template before you attempt to edit it. In Windows, click Start, point to or click Search, and the follow the instructions. On a Macintosh computer, use the finder.

From Word

You open a template for editing the same way you open a regular document by using the Open option on the File menu. The trick is navigating to the correct folder that contains the template. Once you have done this, you can simply open the template. Note that if you have opened the template, the name shown in the title bar will be the template's name, not Document 1. Since the task of navigating to the template is difficult, you may want to simply open the template directly.

From Internet Explorer

You open a template for editing from Microsoft Internet Explorer by right-clicking on it and selecting Open from the shortcut-menu. If you simply double-click the template, you will get a new document based on the template.

Changing the Attached Template

If you move a document to a different computer that doesn't have the template, the attachment will be broken. If you move the template into a different directory on your computer, the attachment will probably be broken. If your template is on a server and you give the server a different name, the attachment will be broken. To change the template attached to a document, click Templates and Add-Ins on the Tools menu.

Attaching a different template gives you access to any AutoText, macros, toolbars and keyboard customizations in the newly-attached template. It does not give you any of the text from the newly-attached template. It does give you access to styles in the newly-attached template but unless you select Automatically update document styles on the Templates and Add-Ins dialog box when you change the attached template, any styles already in use in your document will not be changed by attaching a new template. You will also not get any document layout such as margins (although indents contained in styles will be imported if the style is imported).

If you want the layout features or text from the new template for your document, your best bet is to create a new document based on the new template and then copy the contents of your old document into the new document. Then, close the old document and save your new document using the same name. Note that your new document will use style definitions from the template rather than from your old document.

Using One template to Create a New Template

Once you have a good template, why re-invent it? You may have your letterhead set up the way you want, so why not use that template as a base for your form letter. Probably the best way to do this is to open the template as if to edit it and then save it under a different name before you change anything. Doing a Save As will transfer macros and all styles into new template.

If you change the definition of a style in your new template, that style should continue to be applied to paragraphs that serve the same purpose in the new template as paragraphs using the style of the same name serve in other documents. If the style will serve a different purpose in the new template, it should have a different name, and perhaps be based on an existing style from the old template. Doing things this way makes it much easier to copy text between documents based on different templates.

Copying Items from One Template to Another

This is not as simple because Word has no built-in method for doing it. You first will want to create a global template. You can find more information on copying styles, AutoText entries, and so forth from one template to another, depending on your version of Word, in the following Knowledge Base articles:

326538 HOW TO: Copy Word AutoText Entries Between Computers

196541 WD97: Copying Toolbar Does Not Copy Associated Macros

Using the Organizer

You can copy styles, macros, and toolbars between documents or templates. One of the most effective ways to do this is through the Organizer. The Organizer is a tool built into Word that allows you to quickly copy styles, AutoText, toolbars and macros. You can access the Organizer in one of two ways: In Word97 and Word 2000, from the Format menu, choose Style and then click Organizer; or in Word 2002 and later, from the Tools menu, choose Templates and Add-Ins and then click Organizer.

Note   If a style name that you are copying already exists, you are asked if you want to replace it.

I have found it best when copying styles using the Organizer to copy them three times if any of the styles is based on other styles or is followed by other styles. I'm not sure why this makes a difference, but I've found that clicking on that copy button three times means that these relationships continue in the destination template. I know that when I copy them only once, they do not and the styles are then followed by the Normal style.

Macros in Templates and Documents

Macros end up in templates and documents by being recorded by the user, by adding code from the VBA editor, or by being put there from another macro. Because of the security warning that pops up when Word detects macros, many Word users are very leery of them. This is unfortunate because they are one of the best tools available for getting Word to work the way you want it to work.

It is all a matter of trust though. A bowl of soup can be very tasty and provide needed nourishment. It can also contain poison or a disease. For this reason, I recommend that the macro virus security be set at least at Medium in all versions of Word that have such security (Word 97 and later).

In addition to the utility-type macros that you create to perform various functions in a document, there are also built-in auto available to run, based on the occurrence of certain conditions or events.

Auto Macros

There is a class of macros that can be put into a template or document which run without any notice to the user and without being called by a toolbar or button. These are known as "auto" macros and include:

  • AutoOpen – runs when a document/template opened.
  • AutoClose – runs when a document/template closed.
  • AutoExec – runs when Word starts or Template added as global.
  • AutoExit – runs when Word closed (exited) or Template unloaded as global.
  • AutoNew – runs when a new document created.

Tip You can keep these macros from running by holding down your Shift key while opening (closing) the document / starting (exiting) Word.

The AutoOpen macro runs when an existing document or template is opened (not when a new document is created). If you put an AutoOpen macro in Normal.dot template, the macro will run every time any document (or template) is opened. (It will also run anytime you open Normal.dot.)

If you put an AutoOpen macro in your own template, it will run anytime a document based on the template is opened (so long as the template remains attached). It will also run anytime the template is opened. It will run instead of any AutoOpen macro in Normal.dot.

If you put an AutoOpen macro in a global template, it will run only when the template is opened. You can run a global macro upon the opening of any document by placing the following macro in that document's template:

Sub AutoOpen()
    Application.Run.MacroName:="myGlobalMacro"
End Sub

You can also run a macro in a global template upon opening any document by using the document Open event.

AutoClose macros work the same as AutoOpen macros except that they run when a document or template is closed.

AutoExec macros are for use only in Normal.dot and other global templates. They will run anytime the template containing the macro is loaded. (For Normal.dot and automatically loading global macros, this occurs when Word is started. For other global templates, it is when the template is loaded as an Add-In. AutoExec macros can be in Normal.dot, global.dot, global2.dot, etc. and all of them will run! Normally when you have macros with the same name in multiple active places, only the macro closest to the document runs. For AutoOpen macros, if you had an AutoOpen macro in Normal.dot, the attached template, and the document only the macro in the document would run.) The following is an example of an AutoExec macro that disables the web toolbar.

Sub AutoExec()
    Application.CommandBars("Web").Enabled = False
End Sub

AutoExit macros work like AutoExec macros except that they run when the template is unloaded. For Normal.dot, that means when Word is closed. For globals, it means when Word is closed or when the global is unloaded using Templates and Add-Ins.

AutoNew macros have no use in documents or global templates, only in document templates and in Normal.dot. An AutoNew macro in Normal.dot will run anytime a new document is created. An AutoNew macro in a document template will run whenever a document is created based on the template. If there are AutoNew macros in both Normal.dot and in the document template, the macro in the template will run and that in Normal.dot will not.

The following sample macro can be used to test when a macro is called:

Sub AutoOpen()
    ' Test macro
    MsgBox "The AutoOpen macro in Normal.dot is running"
End Sub

Note that you have to write this macro in the VBA Editor, you can't just record it. If you want to record a macro to test, you could record a macro that types some text, pauses, and then deletes that text.

Troubleshooting Templates

Creating Templates from Flawed Documents

If you are going to share your templates with others, or simply plan on using them to make a number of documents, try to plan and structure them with care. Avoid making a template from any documents converted from a different word processing program or even a much earlier version of Word. Because there is no way to translate a complex document structure feature-for-feature from one program to another, these conversions are prone to document corruption. In Word, even documents created in the current version of Word can sometimes cause problems if they have automatically numbered paragraphs, for instance.

The basic idea of templates is to give you or someone else a boost in creating a new document. If your template is full of errors, those errors will replicate themselves ad infinitum! That isn't something that you need and it definitely isn't the example that you want to set. If what you really want to share is text, try sharing it as an AutoText entry.

To clean up text from a converted document, (save it to HTML (or even text) format, reopen that file, and save it again as a DOC document file. You can then save that new document as your template.

Other Template Issues

Following are some typical issues you may experience using templates:

  • Failure to define and use styles. This is especially a problem if you have numbered paragraphs that use Word's automatic numbering.
  • Template conflicts. Using the same names for macros, styles, and/or AutoText entries in multiple active templates. There are times when you want to do this but usually you don't want one template pre-empting another.
  • Multiple copies of Normal.dot. Unless you have multiple users on a computer and have user profiles activated, there should only be one Normal.dot on your computer. (You should also have multiple Normal.dot files if you have multiple versions of Word on your computer.) If you have user profiles activated, there should only be one version of Normal.dot per user.

Conclusion

In this article, we looked at templates in detail. Templates provide a way to make text, styles, macros, keyboard shortcuts, custom toolbars and AutoText entries available for your applications. You can control when and where these features show up by selecting one of the template types; workgroup, global, and user templates.