Working with Templates

The Templates property on the Application object returns the Templates collection. The Templates collection provides you with access to the templates available in Word. Like most other collections in Word, you can use foreach to iterate over each Template in the Templates collection. You can also use the Templates collection's get_Item method to get to a particular template in the collection, passing by reference an object set to a string for the name of the template or an int 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:my templatesTemplate.dot". Path is a string property that returns the folder the template is in, such as "c:my templates".

Types of 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-5 shows the Templates and Add-ins dialog which illustrates two of the three types. A template can be attached to a documentin 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-5. The Templates and Add-ins dialog showing the attached template and global templates.

A template can also be installed as a global template or add-in. In Figure 8-5, 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.

The third type of template is not shown in this dialog. 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. For example, setting the Document's AttachedTemplate property to change the template attached to a document 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 are closed that are using the attached template. You can also add templates of type wdGlobalTemplate to the Templates collection using the Add method of the AddIns collection.






Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
ISBN: 321334884
EAN: N/A
Year: N/A
Pages: 214
Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net