Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Changing the Type of a Word 2007 Content Control

Office 2007

Summary: Learn how to change content control types in Word 2007, including using custom functions and error handling.

Office Visual How To

Lisa Wollin, Microsoft Corporation

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

Overview

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.

Code It

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.

private void ChangeType()
{
  object docPath = @"c:\ChangeType.docx";

  // Open existing document using docPath variable.
  wordDoc = Application.Documents.Open
    (ref docPath, ref missing, ref missing, 
    ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing, 
    ref missing);

  // Set global content control variables.
  listCC = GetCC("listcc");
  textCC = GetCC("textcc");

  wordDoc.ContentControlOnExit += 
    new Microsoft.Office.Interop.Word
   .DocumentEvents2_ContentControlOnExitEventHandler
    (ContentControl_Exit);
}

ContentControlOnExit Event

When you change the type in the drop-down list and then exit the drop-down list, Word raises the ContentControlOnExit event.

private void ContentControl_Exit(
  Word.ContentControl ContentControl,
  ref bool InUndoRedo)
{
  Word.ContentControl current = ContentControl;

  if (current.Tag == listCC.Tag)
  {
    string type = listCC.Range.Text;
    SetCCType(type);
  }
}

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.

private void SetCCType(string Type)
{
  try
  {
    switch (Type)
    {
      case "Calendar":
          textCC.Type = Word.WdContentControlType
              .wdContentControlDate;
          textCC.Title = "Date Content Control";
          break;

      case "Building Block":
          textCC.Type = Word.WdContentControlType
              .wdContentControlBuildingBlockGallery;
          textCC.Title = "Building Block Gallery Content Control";
          break;

      case "Drop-down List":
          textCC.Type = Word.WdContentControlType
              .wdContentControlDropdownList;
          textCC.Title = "Drop-down List Content Control";
          break;

      case "Combo Box":
          textCC.Type = Word.WdContentControlType
              .wdContentControlComboBox;
          textCC.Title = "Combo Box Content Control";
          break;

      case "Picture":
          textCC.Type = Word.WdContentControlType
              .wdContentControlPicture;
          textCC.Title = "Picture Content Control";
          break;

      case "Rich Text":
          textCC.Type = Word.WdContentControlType
              .wdContentControlRichText;
          textCC.Title = "Rich Text Content Control";
          break;

      case "Plain Text":
          textCC.Type = Word.WdContentControlType
              .wdContentControlText;
          textCC.Title = "Plain Text Content Control";
          break;

      case "Group":
          textCC.Type = Word.WdContentControlType
              .wdContentControlGroup;
          textCC.Title = "Group Content Control";
          break;

      default:
          // Return the default type set for the type variable.
          break;
    }
  }
  catch
  {
    MessageBox.Show("You cannot change this content control " +
      "to the selected type. Please try another type.");
  }
}

Read It

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.

See It

Video splash screen

Watch the Video

Video Length: 00:20:29

File Size: 12.2 MB WMV

Explore It
Show:
© 2014 Microsoft