Using Custom Word 2003 Templates with Document Libraries

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Summary: Learn how to connect Word templates to a document library so that all documents in the document library, whether new or existing, have the same appearance, even with subsequent updates to the template.

Stuart Stuple, Microsoft Corporation

Lisa Wollin, Microsoft Corporation

May 2007

Applies to: Microsoft Office Word 2003, Microsoft Windows SharePoint Services 2.0

Contents

  • Introduction to Document Libraries

  • Using Custom Templates in Document Libraries

    • Create a Custom Document Template

    • Create an XML Expansion Pack and Manifest

    • Add VBA Code to the Template

    • Link the Template to the Document Library

  • Conclusion

  • Additional Resources

Introduction to Document Libraries

Document libraries in Microsoft Windows SharePoint Services 2.0 are ideal for storing collections of documents that you share across one or more teams, and Word templates are perfect for maintaining a consistent appearance and behavior across a set of documents. However, using custom templates with document libraries does not automatically ensure that all documents in the document library look the same.

When you create a document library in Windows SharePoint Services, you usually define the default document template for all new documents that you create in the document library. If you create a typical document library, the default document template is a Microsoft Office Word 2003 document. If all you want is to provide a starting point for users, the standard Word document should be sufficient.

However, if you want to ensure that all the documents in the document library maintain a consistent appearance, and you want to be able to update the look of the documents on an ongoing basis, you probably want to use a custom document template. A custom document template lets you update the appearance of styles used in existing documents without opening the documents or reapplying styles. Additionally, a custom document template lets you provide custom functionality, such as macros, building blocks, and content controls.

Using Custom Templates in Document Libraries

When you open a document template from a Windows SharePoint Services document library, Word downloads a temporary copy of the template to the Temporary Internet Files folder on the local computer. After you close the document, Word deletes the local copy of the template. However, the document continues to reference the local file instead of the template located on the server. This prevents any updates to the original template from affecting the appearance of the document. Therefore any changes that the document library administrator makes to the template only affect documents that you create after the changes and do not affect existing documents.

To ensure that all documents in a document library reference an up-to-date version of the template, you must do the following:

  1. Create a custom document template (if you have not already done this).

  2. Create an XML expansion pack and manifest file.

  3. Create a macro that links new documents to the local template.

  4. Link the custom template to the document library.

Create a Custom Document Template

Create a document template for the library and add it to the forms directory of the document library. There are no limitations to creating a template for a document library. Within the template, you can create styles, form controls, and fields. For more information about creating templates, see the following resources on Office Online:

Note   If you already have a template, you can copy it into the forms directory.

Create an XML Expansion Pack and Manifest

An XML expansion pack is a collection or group of files that constitute an entire solution. For templates in document libraries, the sole purpose of the XML expansion pack is to download and update the local version of the template. This ensures that a user has the most recent version of a template.

The contents of the XML expansion pack are very simple. The XML expansion pack consists of a simple schema file with no content. To create the XML expansion pack, in the forms directory of the document library, create an XML schema file with no content, as follows:

<?xml version="1.0" encoding="utf-8" ?> 
<xs:schema targetNamespace="<-- any namespace -->" xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema>

Next, you need a manifest file for the XML expansion pack. The manifest file contains a list of all the files that are included in the solution. For a custom template in a document library, this list includes the template path and file name in the document library and the schema path and file name for the XML expansion pack that you created in the previous example. The following code shows the markup for a manifest file. Create a similar file for your template and XML expansion pack and copy it into the forms directory of the document library.

<manifest xmlns="http://schemas.microsoft.com/office/xmlexpansionpacks/2003">
  <version>1.0</version>
  <uri><-- XML Schema file targetNamespace above --></uri>
  <solution>
    <solutionID>testSolution</solutionID>
    <type>other</type>
    <alias lcid="*">Template</alias>
    <file>
      <type>template</type>
      <version>1.0</version>
      <filePath><-- template file name --></filePath>
      <templateID><-- {GUID} --></templateID>
    </file>
  </solution>
  <solution>
    <solutionID>testSolution-Schema</solutionID>
    <type>schema</type>
    <alias lcid="*">Template</alias>
    <file>
      <type>schema</type>
      <version>1.0</version>
      <filePath><-- XML Schema file name --></filePath>
    </file>
  </solution>
</manifest>

Note   For Word to be able to read the XML expansion pack, you must digitally sign it with a valid digital certificate. For more information, see Digitally sign an Office document.

For more information about XML expansion packs, see the following resources:

Add VBA Code to the Template

When you create a document for a document library, Word copies the template to the Word template folder on the local computer. However, when you view or edit the document from the browser, the browser automatically creates a copy of the template in a temporary folder, and Word applies the template in the temporary folder to the document. This process causes the template applied to the document to be out of sync with the template on the server. To ensure that the document uses the correct template, you must reapply the correct local template to the document. The best way to do this is by using code.

The following code uses the New event for the Document object to update the template whenever a user creates a new document from the template. Additionally, this code uses the Open event for the Document object to update the template attached to the document every time a user opens the document.

Private Sub Document_New()

  ' Temporarily unlink this file from the template.
  ActiveDocument.AttachedTemplate = ""

  ' Then update the template (the UpdateTemplate function is a separate function).
  UpdateTemplate

End Sub

Sub UpdateTemplate()

  ' If the user has not downloaded the XML expansion pack at least once,
  ' this will fail; On Error Resume Next allows the code to continue.
  On Error Resume Next

  Dim strTemplatePath As String

  ' Get file name for local template - to do this, we need this path:
  ' %USERPROFILE%\Local Settings\Application Data\Microsoft\Schemas\
  ' <namespace>\<solutionId>\< templateFilename >.
  ' IMPORTANT:  You need to specify the <namespace>, <solutionId>, 
  ' and < templateFilename > for your custom template.

  strTemplatePath = Environ("USERPROFILE") & _
    "\Local Settings\Application Data\Microsoft\Schemas\" & _
    "<namespace>" & "\" & _
    "<solutionId> " & "\" & _
    "<templateFilename>"

  ' Re-link document to local template.
  ActiveDocument.AttachedTemplate = strTemplatePath
End Sub

Private Sub Document_Open()
  ' Do nothing if the solution author opens the template directly.
  If (InStr(1, ActiveDocument.FullName, ".dotm", vbTextCompare) = 0) Then

    ' Temporarily unlink this file from the template.
    ActiveDocument.AttachedTemplate = ""

    ' Update template.
    UpdateTemplate

  End If
End Sub

Note   For Word to automatically run this code, you must code sign the document with a valid digital certificate. For more information, see Digitally sign an Office document.

After you create the template, you must link the template to the document library. The following steps show how to do this.

  1. Open the document library in Internet Explorer.

  2. In the list of Actions in the left pane, click Modify settings and columns.

  3. Under General Settings, click Change general settings.

  4. Under Document Template, change the template file name in the Template URL text box. By default, this path looks something like <doclibname>/Forms/template.doc, where <doclibname> is the name of the document library. Change "template.doc" to the name of your template.

After you are finished, when users create or open a document in the document library, Word requests that they download the XML expansion pack.

Because the template is part of the XML expansion pack, you can update the template attached to new and existing documents by doing the following:

  1. Update the file in the SharePoint library.

  2. Increment the version numbers in the XML expansion pack file.

  3. Sign the modified XML expansion pack manifest file with a trusted digital certificate.

For more information about using templates with document libraries in Windows SharePoint Services, see the following additional resources:

Conclusion

Word templates are extremely powerful. They enable you to change the appearance and behavior of all the documents that use them by changing the formatting, features, and code in a single file. When you couple them with document libraries in Windows SharePoint Services, you can take templates to a higher level by making sure that all documents in the document library use and adhere to the same template, including subsequent updates to the template.

Regardless of the type of documents that you maintain, with a custom document template and a Windows SharePoint Services document library, you can ensure a consistent appearance and behavior across all the documents in the library, and the appearance and behavior remains consistent whether you created the document two years ago or two years from now.

Additional Resources