Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath

The Templates property on the Application object returns the Templates collection. The Templates collection provides you access to the templates available in Word. As in most other collections in Word, you can use For Each to iterate over each Template in the Templates collection. You can also use the Templates collection's Item property to get to a particular template in the collection, passing a String for the name of the template or an Integer for the 1-based index into the collection.

You can also get to a Template object by using the Application object's NormalTemplate property, which returns a Template object for normal.dotthe global template that is always open and associated with a document when you have not specified a different template. If you have a Document object and you want to determine what template is associated with it, you can use the Document object's AttachedTemplate. When you get the value of AttachedTemplate, it returns an Object that you can cast to a Template object. When you set the value of AttachedTemplate, you can pass either a Template object or a String containing the filename of the template.

The Template object's OpenAsDocument method enables you to open a template as a document and edit it. The Name property is a String property that returns the name of the template, such as "Template.dot". FullName is a String property that returns the complete filename of the template, such as "c:\mytemplates\Template.dot". Path is a String property that returns the folder the template is in, such as "c:\my templates".

The Template object's Type property returns a member of the WdTemplateType enumeration that designates the type of the template. A template can be one of three types. Figure 8.3 shows the Templates and Add-ins dialog box, which illustrates two of the three types. A template can be attached to a document; in this case, the template AWtemplate.dot is attached to the active document. A template attached to a document has a type of wdAttachedTemplate. The Templates collection will contain an attached template only while the document the template is attached to is opened. When the document associated with the template is closed, the Template object attached to that document will no longer be in the Templates collection (unless, of course, it is attached to another document that is still open).

Figure 8.3. The Templates and Add-ins dialog box, showing the attached template and global templates.

A template can also be installed as a global template or add-in. In Figure 8.3, the template SnagIt Add-in.dot is a global add-in template. A global template has a type of wdGlobalTemplate. Templates installed in this way are often acting as a simple add-in, providing toolbars or additional menu commands to Word. A template of this type will always be in the Templates collection until it is uninstalled or removed using the Templates and Add-ins dialog box.

The third type of template is not shown in this dialog box. The normal.dot template is always open in Word and is of type wdNormalTemplate. This template is always present in the Templates collection.

The Templates collection does not have an Add method. Templates are added indirectly through actions you take with other objects. Setting the Document's AttachedTemplate property to change the template attached to a document, for example, adds the template to the Templates collection if it is not already there. Opening a document that has an attached template not already in the Templates collection adds the attached template to the Templates collection. Templates with type wdAttachedTemplate are removed from the Templates collection when all documents that are using the attached template are closed. You can also add templates of type wdGlobalTemplate to the Templates collection using the Add method of the AddIns collection.

Категории