Export (0) Print
Expand All
Expand Minimize

Filtering the Building Block List in a Word 2007 Content Control

Office 2007

Office Visual How To

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

Overview

You can create content controls that use building blocks to present rich content selections to users in Microsoft Office Word 2007. You can help users by indicating what content they can insert and by limiting their choices. You do this by filtering the list of building blocks that are available for a content control. In this video, you learn how to create building block content controls to present content selections to the user. You also learn how to programmatically change the building block gallery and category in response to events.

Code It

There are two ways to filter the building block list for a content control: You can set the filtering when you create the content control, or you can set the filtering in response to an event.

To filter a building block list for a content control, you first need to have a building block gallery content control. Then you apply a building block gallery to the content control. To do this, you use the BuildingBlockType property. You can further filter a building block content control by setting the BuildingBlockCategory property to a category within the gallery.

The following example programmatically creates a building block content control, and then sets the building block gallery and category to use for filtering the building block list.

private void SetBuildingBlockGallery()
{
  string strTitle = "Building Block Content Control";

  Word.ContentControl objCC = Application.ActiveDocument
    .ContentControls.Add(Word.WdContentControlType
    .wdContentControlBuildingBlockGallery, ref missing);

  objCC.Title = strTitle;
  objCC.BuildingBlockType = Word.WdBuildingBlockTypes
    .wdTypeCustom1;
}

You can use this same concept to programmatically set the building block gallery and category for a content control in response to an event. For example, perhaps you have several content controls in a document, and you want to set the building block list of one of the content controls based on the value that a user enters into another content control.

To do this, you use the ContentControlOnEnter or ContentControlOnExit event.

The following code example creates a new document from a custom template. The custom template contains several content controls. One of the content controls, the teacher’s name, displays a filtered building block list based on the value that the user enters into another content control, the class grade level.

The content control for the grade level lists third, fourth, and fifth grades. When the user selects one of these grade levels, the teacher name content control lists only the teachers that teach that grade.

After creating the new document, the code adds an error handler for the ContentControlOnExit event. Then in the ContentControlOnExit event, if the content control that the user is exiting is the grade level content control, the code sets the category based on the value that the user selects and applies the gallery and category to the teacher name content control.

private void SetBuildingBlockGalleryOnTheFly()
{
  object filename = "c:\\template.dotx";
  wordDoc = Application.Documents
    .Add(ref filename, ref missing,
    ref missing, ref missing);
  wordDoc.ContentControlOnExit += 
    new Microsoft.Office.Interop.Word
    .DocumentEvents2_ContentControlOnExitEventHandler
    (ContentControl_Exit);
}

protected void ContentControl_Exit(
  Word.ContentControl ContentControl, 
  ref bool InUndoRedo)
{
  Word.ContentControl tempCC = ContentControl;

  switch (tempCC.Tag)
  {
    case "gradeLevel":
      string category;
      switch (tempCC.Range.Text)
      {
        case "3":
          category = "3rd Grade Teachers";
          break;
        case "4":
          category = "4th Grade Teachers";
          break;
        case "5":
          category = "5th Grade Teachers";
          break;
        default:
          category = "";
          break;
      }

      Word.ContentControl cc = GetCC("teacherName");

      if (cc != null)
      {
        cc.BuildingBlockType = Microsoft.Office
        .Interop.Word.WdBuildingBlockTypes
          .wdTypeCustom1;
        cc.BuildingBlockCategory = category;
      }
      break;
    default:
      break;
    }
}

private Word.ContentControl GetCC(string Tag)
{
  foreach (Word.ContentControl cc in Application
    .ActiveDocument.ContentControls)
  {
    if (cc.Tag == Tag)
    {
      return cc;
    }
  }

  return null;
}

For a complete explanation of the code and a demonstration of the finished template with the active code, watch the video.

NoteNote
This Visual How-to uses the lesson plan template located on Office Online. Content controls and building blocks are then added to the template.
See It

Filtering Building Blocks In A Content Control

Watch the Video

Video Length: 00:06:03

File Size: 7.0 MB WMV

Explore It
Show:
© 2014 Microsoft