Changing the Type of a Word 2007 Content Control
Summary: Learn how to change content control types in Word 2007, including using custom functions and error handling.
Lisa Wollin, Microsoft Corporation
Applies to: 2007 Microsoft Office System, Microsoft Office Word 2007
Content controls in Microsoft Office Word 2007 are very flexible. In most cases, you can easily switch a content control from one type to another. For example, if you have a date content control, you can change it to a text content control. To change a content control to a different type, you use the Type property of the ContentControl object and set it to one of the WdContentControlType constants. The example in this topic shows how this process works. It uses a document that contains two content controls. One content control is a drop-down list that lists the different types of content controls; the other content control can be any type of content control. When you select a content control type from the drop-down list, the second content control changes to the new content control type. There are three main parts to this example: two custom functions, ChangeType and SetCCType, and one event handler for the ContentControlOnExit event. ChangeType Function The following code shows the ChangeType function. This function loads the document with the two content controls, sets global variables for the two content controls in the document, and adds an event handler for the ContentControlOnExit event.
ContentControlOnExit Event When you change the type in the drop-down list and then exit the drop-down list, Word raises the ContentControlOnExit event.
SetCCType Function The SetCCType function has a Type parameter that is a string. This value corresponds to the value of the drop-down list. Based on the value, the SetCCType function changes the Type and Title of the second content control.
Error Handling You can change most content controls into most other types of content controls, but there are some exceptions. For example, if you try to change any type of content control into a picture content control, Word usually raises an error. However, if you have a rich text content control with an image in it, you can usually change the type to a picture content control without raising an exception. There is not a comprehensive list of conditions for changing from one type of another, so adding error handling is important to keeping your solution functioning correctly. |
Video Length: 00:20:29 File Size: 12.2 MB WMV |