共用方式為


Building Word 2007 Document Templates Using Content Controls

Office Visual How To

Erika Ehrli, Lisa Wollin, Brian Jones, Microsoft Corporation

Applies to: 2007 Microsoft Office System, Microsoft Office Word 2007

Overview

Content controls are bounded and potentially labeled regions in a document that serve as containers for specific types of content. Individual content controls can contain content such as dates, lists, or paragraphs of formatted text. They enable you to create rich, structured blocks of content. Content controls enable you to create templates that insert well-defined blocks into your documents. Content controls also build on the custom XML support introduced in Microsoft Office Word 2003.

Code It

Whether you want to add or delete content controls or access and manipulate existing content controls, you can do it with code.

Adding a Content Control

Use the Add method of the ContentControls collection to add a content control to a document. The following example adds a date picker to the active document and sets the date value to the current date.

You can use the same basic construction to add any of the different types of content controls to a document.

Adding a Title to a Content Control

Use the Title property to add a title to a content control. This is text that users see, and it can help them to know what type of data to enter into the content control. The following example adds a new plain-text content control to the active document and sets the title, or display text, for the control.

Modifying Placeholder Text for a Content Control

Placeholder text is temporary text. It can be a simple one-word or two-word description (similar to the title) or it can be a more thorough description (such as numbered steps). The way that you modify placeholder text is the same regardless of the type of content control or the expected contents of the content control. The following example adds a drop-down list to the active document, sets the placeholder text for the control, and then fills the list with the names of several animals.

Read It

Content controls enable you to create templates that insert well-defined blocks into your documents. Content controls enable you to:

  • Specify structured regions in a template. Each structured region has its own unique ID so that you can read from and write to it. Examples of types of structured regions (or content controls) are combo boxes, pictures, text blocks, and calendars.

  • Determine the behavior of content controls. Each content control takes up a portion of a document and, as the template author, you can specify what each region does. For example, if you want a region of your template to be a calendar, you insert a calendar content control in that area of the document, which automatically determines what that block of content does. Similarly, if you want a section of a template to display an image, create a picture content control in that area. In this way, you can build a template with predefined block types.

  • Restrict the content of content controls. You can restrict the content of each content control, so that it cannot be deleted or edited. This is useful if, for example, you have copyright information in a template that the user should be able to read but not edit. You can also lock a template's content so that a user does not accidentally delete portions of it. This makes templates more robust than in previous versions.

  • Map the contents of a content control to data in a custom XML part that is stored with the document. For example, if you insert a document parts content control that contains a table of stock prices, you can map the table cells to nodes in an XML file that contain the current stock prices. When the prices change, an add-in can programmatically update the attached XML file, which is bound to each cell, and the new, updated prices automatically appear in the table.

The easiest way to create a content control is through the user interface (UI) (although you can also create them programmatically). To create a content control through the UI, select the text that you want to turn into a content control and then choose the content control type you want from the content controls section of the Developer ribbon. This creates a content control around the selected text.

Types of Content Controls

There are seven different types of content controls that you can add to a document, each of which is represented in a new enumeration called WdContentControlType.

Table 1. Content controls in WdContentControlType
ControlDefinition
CalendarA date-time picker.
Building BlockEnables the user to choose from specified building blocks.
Drop-Down ListA drop-down list.
Combo BoxA combo box.
PictureA picture.
Rich TextA block of rich text.
Plain TextA block of plain text.
See It To watch video, click link below image

Watch the Video

Video Length: 00:09:51

File Size: 13.0 MB

File Type: WMV file

Explore It