Updated Word Content Control Topics for the Visual Studio Code Name "Orcas" January CTP

Today the VSTO user education team is presenting 3 more topics from the VSTO documentation included with the January 2007 CTP of Microsoft Visual Studio Code Name “Orcas” (for more information, see yesterday's post). Today's topics are all about Word content controls: "How to: Add Content Controls to Word Documents", "How to: Protect Parts of Documents by Using Content Controls", and "Walkthrough: Creating a Template by Using Content Controls". If you have any feedback regarding these topics, please leave us a comment.

*********************************************************************************************************************************

How to: Add Content Controls to Word Documents

You can add content controls to a Microsoft Office Word 2007 document at design time or at run time. For more information about content controls, see "Content Controls".

Adding Content Controls at Design Time

There are several ways to add content controls to your document at design time:

· Add a content control from the Word Controls tab of the Toolbox to your document.

· Add a content control to your document in the same manner you would add a native content control in Word.

· Drag a content control to your document from the Data Sources window in the same way you would add a Windows Forms control from the Data Sources window. This is useful when you want to bind the control to data when the control is created. For more information, see "How to: Populate Documents with Data from Objects" and "How to: Populate Documents with Data from a Local Database File".

Note The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, select Import and Export Settings on the Tools menu. For more information, see "Visual Studio Settings".

To add a content control to a document by using the Toolbox

1. In the document that is hosted in the Visual Studio designer, move the cursor to the location you want to add the content control, or select the text that you want the content control to replace.

2. Open the Toolbox and click the Word Controls tab.

3. Perform one of the following commands:

· Double-click a content control in the Toolbox.

-or-

· Click a content control in the Toolbox and then press the ENTER key.

-or-

· Drag a content control from the Toolbox to the document. The content control is added at the current selection in the document, not at the location of the mouse pointer.

Note You cannot add a Microsoft.Office.Tools.Word.GroupContentControl by using the Toolbox. You can only add a Microsoft.Office.Tools.Word.GroupContentControl in Word, or at run time.

To add a content control to a document in Word

1. In the document that is hosted in the Visual Studio designer, move the cursor to the location you want to add the content control, or select the text that you want the content control to replace.

2. On the Ribbon, click the Developer tab.

3. In the Controls group, click the icon for the content control that you want to add.

Adding Content Controls at Run Time

You can add content controls programmatically to your document at run time by using methods of the Microsoft.Office.Tools.Word.ControlCollection class. Each method has three overloads that you can use to add a content control in the following ways:

· Add a control at the current selection.

· Add a control at a specified range.

· Add a control that is based on a native content control in the document.

For more information about the methods that you can use to add content controls at run time, see "Adding Controls to Office Documents at Run Time" and "Helper Methods for Host Controls".

To add a content control at the current selection

1. Use a Microsoft.Office.Tools.Word.ControlCollection method that has the name Add<control class> (where control class is the class name of the content control that you want to add, such as Microsoft.Office.Tools.Word.ControlCollection.AddRichTextContentControl), and that has a single parameter for the name of the new control.

The following code example adds a Microsoft.Office.Tools.Word.RichTextContentControl to the current selection, which is at the end of the paragraph in the document. To run this code, add the code to the ThisDocument class in your project, and call the AddRichTextControlAtSelection method from the ThisDocument_Startup event handler.

[Visual Basic]

Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl

Private Sub AddRichTextControlAtSelection()

Me.Paragraphs(1).Range.InsertParagraphBefore()

Me.Paragraphs(1).Range.Text = "What is your first name? " & vbCrLf

Me.Paragraphs(1).Range.Characters.Last.Select()

Dim sel As Word.Selection = Application.Selection

sel.Collapse(Word.WdCollapseDirection.wdCollapseStart)

richTextControl1 = Me.Controls.AddRichTextContentControl("richTextControl1")

richTextControl1.PlaceholderText = "Enter first name"

End Sub

[C#]

private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;

private void AddRichTextControlAtSelection()

{

this.Paragraphs[1].Range.InsertParagraphBefore();

this.Paragraphs[1].Range.Text = "What is your first name? \n";

this.Paragraphs[1].Range.Characters.Last.Select();

object collapseDirection = Word.WdCollapseDirection.wdCollapseStart;

Word.Selection sel = this.Application.Selection;

sel.Collapse(ref collapseDirection);

richTextControl1 = this.Controls.AddRichTextContentControl("richTextControl1");

richTextControl1.PlaceholderText = "Enter first name";

}

To add a content control at a specified range

1. Use a Microsoft.Office.Tools.Word.ControlCollection method that has the name Add<control class> (where control class is the name of the content control class that you want to add, such as Microsoft.Office.Tools.Word.ControlCollection.AddRichTextContentControl), and that has a Microsoft.Office.Interop.Word.Range parameter.

The following code example adds a Microsoft.Office.Tools.Word.RichTextContentControl to the first paragraph in the document by specifying a range. To run this code, add the code to the ThisDocument class in your project, and call the AddRichTextControlAtRange method from the ThisDocument_Startup event handler.

[Visual Basic]

Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl

Private Sub AddRichTextControlAtRange()

Me.Paragraphs(1).Range.InsertParagraphBefore()

Me.Paragraphs(1).Range.Text = "What is your first name? " & vbCrLf

Dim range1 As Word.Range = Me.Paragraphs(1).Range.Characters.Last

richTextControl2 = Me.Controls.AddRichTextContentControl(range1, "richTextControl2")

richTextControl2.PlaceholderText = "Enter first name"

End Sub

[C#]

private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;

private void AddRichTextControlAtRange()

{

this.Paragraphs[1].Range.InsertParagraphBefore();

this.Paragraphs[1].Range.Text = "What is your first name? \n";

Word.Range range1 = this.Paragraphs[1].Range.Characters.Last;

richTextControl2 = this.Controls.AddRichTextContentControl(range1,

"richTextControl2");

richTextControl2.PlaceholderText = "Enter first name";

}

To add a content control that is based on a native content control

1. Use a Microsoft.Office.Tools.Word.ControlCollection method that has the name Add<control class> (where control class is the name of the content control class that you want to add, such as Microsoft.Office.Tools.Word.ControlCollection.AddRichTextContentControl), and that has a Microsoft.Office.Interop.Word.ContentControl parameter.

The following code example creates a Microsoft.Office.Tools.Word.RichTextContentControl for every native rich text content control in the document. To run this code, add the code to the ThisDocument class in your project, and call the CreateRichTextControlsFromNativeControls method from the ThisDocument_Startup event handler.

[Visual Basic]

Private richTextControls As New System.Collections.Generic.List _

(Of Microsoft.Office.Tools.Word.RichTextContentControl)

Private Sub CreateRichTextControlsFromNativeControls()

If Me.ContentControls.Count <= 0 Then

Return

End If

Dim count As Integer = 0

For Each nativeControl As Word.ContentControl In Me.ContentControls

If nativeControl.Type = Word.WdContentControlType.wdContentControlRichText Then

count += 1

Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _

Me.Controls.AddRichTextContentControl(nativeControl, _

"VSTORichTextContentControl" + count.ToString())

richTextControls.Add(tempControl)

End If

Next nativeControl

End Sub

[C#]

private System.Collections.Generic.List

<Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;

private void CreateRichTextControlsFromNativeControls()

{

if (this.ContentControls.Count <= 0)

return;

richTextControls = new System.Collections.Generic.List

<Microsoft.Office.Tools.Word.RichTextContentControl>();

int count = 0;

foreach (Word.ContentControl nativeControl in this.ContentControls)

{

if (nativeControl.Type ==

Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)

{

count++;

Microsoft.Office.Tools.Word.RichTextContentControl tempControl =

this.Controls.AddRichTextContentControl(nativeControl,

"VSTORichTextControl" + count.ToString());

richTextControls.Add(tempControl);

}

}

}

*************************************

How to: Protect Parts of Documents by Using Content Controls

When you protect part of a document, you prevent users from changing or deleting the content in that part of the document. There are several ways you can protect parts of a Microsoft Office Word 2007 document by using content controls:

· You can protect a content control.

· You can protect a part of a document that is not in a content control.

For more information about how to protect Word 2007 documents by using content controls, see "Content Controls".

Protecting a Content Control

You can prevent users from editing or deleting a content control by setting properties of the control at design time or at run time.

To prevent users from editing or deleting a content control at design time

1. In the document that is hosted in the Visual Studio designer, select the content control that you want to protect.

2. On the Ribbon, click the Developer tab.

3. In the Controls group, click Properties.

4. In the Content Control Properties dialog box, select one or both of the following options:

· To prevent users from editing the control, select Contents cannot be edited.

· To prevent users from deleting the control, select Content control cannot be deleted.

5. Click OK.

To prevent users from editing or deleting a content control at run time

1. Set the LockContents property of the content control to true to prevent users from editing the control, and set the LockContentControl property to true to prevent users from deleting the control.

The following code example demonstrates the LockContents and LockContentControl properties of two different Microsoft.Office.Tools.Word.RichTextContentControl objects. One of the controls can be edited but not deleted, and the other control can be deleted but not edited. To run this code, add the code to the ThisDocument class in your project, and call the AddProtectdContentControls method from the ThisDocument_Startup event handler.

[Visual Basic]

Dim deletableControl As Microsoft.Office.Tools.Word.RichTextContentControl

Dim editableControl As Microsoft.Office.Tools.Word.RichTextContentControl

Private Sub AddProtectedContentControls()

Me.Paragraphs(1).Range.InsertParagraphBefore()

Dim range1 As Word.Range = Me.Paragraphs(1).Range

deletableControl = Me.Controls.AddRichTextContentControl(range1, _

"deletableControl")

deletableControl.PlaceholderText = "You can delete this control, " & _

"but you cannot edit it"

deletableControl.LockContents = True

range1.InsertParagraphAfter()

Dim range2 As Word.Range = Me.Paragraphs(2).Range

editableControl = Me.Controls.AddRichTextContentControl(range2, _

"editableControl")

editableControl.PlaceholderText = "You can edit this control, " & _

"but you cannot delete it"

editableControl.LockContentControl = True

End Sub

[C#]

private Microsoft.Office.Tools.Word.RichTextContentControl deletableControl;

private Microsoft.Office.Tools.Word.RichTextContentControl editableControl;

private void AddProtectedContentControls()

{

this.Paragraphs[1].Range.InsertParagraphBefore();

Word.Range range1 = this.Paragraphs[1].Range;

deletableControl = this.Controls.AddRichTextContentControl(range1,

"deletableControl");

deletableControl.PlaceholderText = "You can delete this control, " +

"but you cannot edit it";

deletableControl.LockContents = true;

range1.InsertParagraphAfter();

Word.Range range2 = this.Paragraphs[2].Range;

editableControl = this.Controls.AddRichTextContentControl(range2,

"editableControl");

editableControl.PlaceholderText = "You can edit this control, " +

"but you cannot delete it";

editableControl.LockContentControl = true;

}

Protecting a Part of a Document That Is Not in a Content Control

You can prevent users from changing an area of a document by putting the area in a Microsoft.Office.Tools.Word.GroupContentControl. This is useful in the following scenarios:

· You want to protect an area that does not contain content controls.

· You want to protect an area that already contains content controls, but the text or other items that you want to protect are not in the content controls.

Note If you create a Microsoft.Office.Tools.Word.GroupContentControl that contains embedded content controls, the embedded content controls are not automatically protected. To prevent users from editing an embedded content control, use the LockContents property of the control.

To protect an area of a document at design time

1. In the document that is hosted in the Visual Studio designer, select the area that you want to protect.

2. On the Ribbon, click the Developer tab.

3. In the Controls group, click the Group drop-down button, and then click Group.

A Microsoft.Office.Tools.Word.GroupContentControl that contains the protected region is automatically generated in the ThisDocument class in your project. A border that represents the group control is visible at design time, but there is no visible border at run time.

To protect an area of a document at run time

1. Programmatically select the area that you want to protect, and then call the Microsoft.Office.Tools.Word.ControlCollection.AddGroupContentControl method to create a Microsoft.Office.Tools.Word.GroupContentControl.

The following code example adds text to the first paragraph in the document, selects the first paragraph, and then instantiates a Microsoft.Office.Tools.Word.GroupContentControl. To run this code, add the code to the ThisDocument class in your project, and call the ProtectFirstParagraph method from the ThisDocument_Startup event handler.

[Visual Basic]

Dim groupControl1 As Microsoft.Office.Tools.Word.GroupContentControl

Private Sub ProtectFirstParagraph()

Me.Paragraphs(1).Range.InsertParagraphBefore()

Dim range1 As Word.Range = Me.Paragraphs(1).Range

range1.Text = "You cannot edit or change the formatting of text " & _

"in this paragraph, because this paragraph is in a GroupContentControl."

range1.Select()

groupControl1 = Me.Controls.AddGroupContentControl("groupControl1")

End Sub

[C#]

private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;

private void ProtectFirstParagraph()

{

this.Paragraphs[1].Range.InsertParagraphBefore();

Word.Range range1 = this.Paragraphs[1].Range;

range1.Text = "You cannot edit or change the formatting of text " +

"in this sentence, because this sentence is in a GroupContentControl.";

range1.Select();

groupControl1 = this.Controls.AddGroupContentControl("groupControl1");

}

***************************************************************************************

Walkthrough: Creating a Template By Using Content Controls

This walkthrough demonstrates how to use content controls to create structured and reusable content in a Microsoft Office Word 2007 template.

Word enables you to create a collection of reusable document parts, named building blocks. During this walkthrough, you create two tables as building blocks. Each table contains several content controls that can hold different types of content, such as plain text or dates. One of the tables contains information about an employee, and the other table contains customer feedback.

When you create a document from the template, you can add either of the tables to the document by using several Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl objects, which display the available building blocks in the template.

This walkthrough illustrates the following tasks:

· Creating tables that contain content controls in a Word template at design time.

· Populating a combo box content control and a drop-down list content control programmatically.

· Preventing users from editing a specified table.

· Adding tables to the building block collection of a template.

· Creating a content control that displays the available building blocks in the template.

Note The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, select Import and Export Settings on the Tools menu. For more information, see "Visual Studio Settings".

Creating a New Word Template Project

Create a Word 2007 template so that users can create their own copies easily.

To create a new Word 2007 template project

1. Start Visual Studio.

2. Create a Word Template project with the name MyBuildingBlockTemplate. Make sure that you use the Word Template project template for the 2007 Microsoft Office system, and that you create a new document in the solution. For more information, see "How to: Create Visual Studio Tools For Office Projects".

Visual Studio opens the new Word template in the designer and adds the MyBuildingBlockTemplate project to Solution Explorer.

Creating the Employee Table

Create a table that contains four different types of content controls where the user can enter information about an employee.

To create the employee table

1. In the Word template that is hosted in the Visual Studio designer, on the Ribbon, click the Insert tab.

2. In the Tables group, click Table, and insert a table with 2 columns and 4 rows.

3. Type text in the first column so that it resembles the following column:

Employee Name

Hire Date

Title

Picture

4. Click in the first cell in the second column.

5. On the Ribbon, click the Developer tab.

6. In the Controls group, click the Text button to add a Microsoft.Office.Tools.Word.PlainTextContentControl to the first cell.

7. Click the second cell in the second column.

8. In the Controls group, click the Date Picker button to add a Microsoft.Office.Tools.Word.DatePickerContentControl to the second cell.

9. Click the third cell in the second column.

10. In the Controls group, click the Combo Box button to add a Microsoft.Office.Tools.Word.ComboBoxContentControl to the third cell.

11. Click the last cell in the second column.

12. In the Controls group, click the Picture Content Control button to add a Microsoft.Office.Tools.Word.PictureContentControl to the last cell.

Creating the Customer Feedback Table

Create a table that contains three different types of content controls where the user can enter customer feedback information.

To create the customer feedback table

1. In the Word template, click in the line after the employee table that you added earlier, and press ENTER to add a second paragraph.

2. On the Ribbon, click the Insert tab.

3. In the Tables group, click Table, and insert a table with 2 columns and 3 rows.

4. Type text in the first column so that it resembles the following column:

Customer Name

Satisfaction Rating

Comments

5. Click in the first cell of the second column.

6. On the Ribbon, click the Developer tab.

7. In the Controls group, click the Text button to add a Microsoft.Office.Tools.Word.PlainTextContentControl to the first cell.

8. Click in the second cell of the second column.

9. In the Controls group, click the Drop-Down List button to add a Microsoft.Office.Tools.Word.DropDownListContentControl to the second cell.

10. Click in the last cell of the second column.

11. In the Controls group, click the Rich Text button to add a Microsoft.Office.Tools.Word.RichTextContentControl to the last cell.

Populating the Combo Box and Drop Down List Programmatically

You can initialize content controls at design time by using the Properties window in Visual Studio or the Controls group in Word. You can also initialize them at run time, which enables you to set their initial states dynamically. For this walkthrough, use code to populate the entries in the Microsoft.Office.Tools.Word.ComboBoxContentControl and Microsoft.Office.Tools.Word.DropDownListContentControl at run time so that you can see how these objects work.

To modify the UI of the content controls programmatically

1. In Solution Explorer, right-click ThisDocument.cs or ThisDocument.vb, and then click View Code.

2. Add the following code to the ThisDocument class. This code declares several objects that you will use later in this walkthrough.

[Visual Basic]

Private GroupControl1 As Microsoft.Office.Tools.Word.GroupContentControl

Private BuildingBlockControl1 As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl

Private BuildingBlockControl2 As Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl

[C#]

private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;

private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl1;

private Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl buildingBlockControl2;

3. Add the following code to the ThisDocument_Startup method of the ThisDocument class. This code adds entries to the Microsoft.Office.Tools.Word.ComboBoxContentControl and Microsoft.Office.Tools.Word.DropDownListContentControl in the tables and sets the placeholder text that is displayed in each of these controls before the user edits them.

[Visual Basic]

ComboBoxContentControl1.PlaceholderText = "Choose a title, or enter your own"

ComboBoxContentControl1.DropdownListEntries.Add("Engineer", "Engineer", 0)

ComboBoxContentControl1.DropdownListEntries.Add("Designer", "Designer", 1)

ComboBoxContentControl1.DropdownListEntries.Add("Manager", "Manager", 2)

DropDownListContentControl1.PlaceholderText = _

"Choose a rating (1 lowest, 3 highest)"

DropDownListContentControl1.DropdownListEntries.Add("1", "1", 0)

DropDownListContentControl1.DropdownListEntries.Add("2", "2", 1)

DropDownListContentControl1.DropdownListEntries.Add("3", "3", 2)

[C#]

comboBoxContentControl1.PlaceholderText = "Choose a title, or enter your own";

comboBoxContentControl1.DropdownListEntries.Add("Engineer", "Engineer", 0);

comboBoxContentControl1.DropdownListEntries.Add("Designer", "Designer", 1);

comboBoxContentControl1.DropdownListEntries.Add("Manager", "Manager", 2);

dropDownListContentControl1.PlaceholderText =

"Choose a rating (1 lowest, 3 highest)";

dropDownListContentControl1.DropdownListEntries.Add("1", "1", 0);

dropDownListContentControl1.DropdownListEntries.Add("2", "2", 1);

dropDownListContentControl1.DropdownListEntries.Add("3", "3", 2);

Preventing Users from Editing the Employee Table

Use the Microsoft.Office.Tools.Word.GroupContentControl object that you declared earlier to protect the employee table. After protecting the table, users can still edit the content controls in the table. However, they cannot edit text in the first column or modify the table in other ways, such as adding or deleting rows and columns. For more information about how to use a Microsoft.Office.Tools.Word.GroupContentControl to protect a part of a document, see "Content Controls".

To prevent users from editing the employee table

1. Add the following code to the ThisDocument_Startup method of the ThisDocument class, after the code that you added in the previous step. This code prevents users from editing the employee table by putting the table inside the Microsoft.Office.Tools.Word.GroupContentControl object that you declared earlier.

[Visual Basic]

Me.Tables(1).Select()

GroupControl1 = Me.Controls.AddGroupContentControl("groupControl1")

[C#]

this.Tables[1].Range.Select();

groupControl1 = this.Controls.AddGroupContentControl("groupControl1");

Adding the Tables to the Building Block Collection

Add the tables to a collection of document building blocks in the template so that users can insert the tables that you have created into the document. For more information about document building blocks, see "Content Controls".

To add the tables to the building blocks in the template

1. Add the following code to the ThisDocument_Startup method of the ThisDocument class, after the code that you added in the previous step. This code adds new building blocks that contain the tables to the Microsoft.Office.Interop.Word.BuildingBlockEntries collection, which contains all the reusable building blocks in the template. The new building blocks are defined in a new category named Employee and Customer Information and are assigned the building block type Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeCustom1.

[Visual Basic]

Dim template1 As Word.Template = TryCast(Me.AttachedTemplate, Word.Template)

If template1 IsNot Nothing Then

template1.BuildingBlockEntries.Add("Employee Table", _

Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information", _

Me.Tables(1).Range, InsertOptions:=Word.WdDocPartInsertOptions.wdInsertContent)

template1.BuildingBlockEntries.Add("Customer Table", _

Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information", _

Me.Tables(2).Range, InsertOptions:=Word.WdDocPartInsertOptions.wdInsertContent)

End If

[C#]

Word.Template template1 = this.AttachedTemplate as Word.Template;

if (template1 != null)

{

object description = null;

template1.BuildingBlockEntries.Add("Employee Table",

Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information",

this.Tables[1].Range, ref description, Word.WdDocPartInsertOptions.wdInsertContent);

template1.BuildingBlockEntries.Add("Customer Table",

Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information",

this.Tables[2].Range, ref description, Word.WdDocPartInsertOptions.wdInsertContent);

}

2. Add the following code to the ThisDocument_Startup method of the ThisDocument class, after the code that you added in the previous step. This code deletes the tables from the template. The tables are no longer necessary, because you have added them to the gallery of reusable building blocks in the template. The code first puts the document into design mode so that the protected employee table can be deleted.

[Visual Basic]

If Me.DesignMode = False Then

Me.ToggleFormsDesign()

End If

Me.Tables(2).Delete()

Me.Tables(1).Delete()

Me.ToggleFormsDesign()

[C#]

if (!this.DesignMode)

{

this.ToggleFormsDesign();

}

this.Tables[2].Delete();

this.Tables[1].Delete();

this.ToggleFormsDesign();

Creating a Content Control that Displays the Building Blocks

Create a content control that provides access to the building blocks (that is, the tables) that you created earlier. Users can click this control to add the tables to the document.

To create a content control that displays the building blocks

1. Add the following code to the ThisDocument_Startup method of the ThisDocument class, after the code that you added in the previous step. This code initializes the Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl object that you declared earlier so that users can click it to add the employee and customer feedback tables to the document. The Microsoft.Office.Tools.Word.BuildingBlockGalleryContentControl displays all building blocks that are defined in the category Employee and Customer Information and that have the building block type Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeCustom1.

[Visual Basic]

BuildingBlockControl1 = Me.Controls.AddBuildingBlockGalleryContentControl( _

Me.Paragraphs(1).Range, "buildingBlockControl1")

BuildingBlockControl1.BuildingBlockCategory = "Employee and Customer Information"

BuildingBlockControl1.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1

BuildingBlockControl1.PlaceholderText = "Choose your first building block"

BuildingBlockControl2 = Me.Controls.AddBuildingBlockGalleryContentControl( _

Me.Paragraphs(2).Range, "buildingBlockControl2")

BuildingBlockControl2.BuildingBlockCategory = "Employee and Customer Information"

BuildingBlockControl2.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1

BuildingBlockControl2.PlaceholderText = "Choose your second building block"

[C#]

buildingBlockControl1 = this.Controls.AddBuildingBlockGalleryContentControl(

this.Paragraphs[1].Range, "buildingBlockControl1");

buildingBlockControl1.BuildingBlockCategory = "Employee and Customer Information";

buildingBlockControl1.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1;

buildingBlockControl1.PlaceholderText = "Choose your first building block";

buildingBlockControl2 = this.Controls.AddBuildingBlockGalleryContentControl(

this.Paragraphs[2].Range, "buildingBlockControl2");

buildingBlockControl2.BuildingBlockCategory = "Employee and Customer Information";

buildingBlockControl2.BuildingBlockType = Word.WdBuildingBlockTypes.wdTypeCustom1;

buildingBlockControl2.PlaceholderText = "Choose your second building block";

Testing the Project

Users can click the building block gallery controls in the document to insert the employee table or the customer feedback table. Users can type or select responses in the content controls in both of the tables. Users can modify other parts of the customer feedback table, but they should not be able to modify other parts of the employee table.

To test the employee table

1. Press F5 to run the project.

2. Click Choose your first building block to display the first building block gallery content control.

3. Click the drop-down arrow next to the Custom Gallery 1 heading in the control, and select Employee Table.

4. Click in the cell to the right of the Employee Name cell and type a name.

Verify that you can add only text to this cell. The Microsoft.Office.Tools.Word.PlainTextContentControl allows users to add only text, not other types of content such as art or a table.

5. Click in the cell to the right of the Hire Date cell and select a date in the date picker.

6. Click in the cell to the right of the Title cell and select one of the job titles in the combo box.

Optionally, type the name of a job title that is not in the list. This is possible because the Microsoft.Office.Tools.Word.ComboBoxContentControl enables users to select from a list of entries or to type their own entries.

7. Click the icon in the cell to the right of the Picture cell and browse to an image to display it.

8. Try to add rows or columns to the table, and try to delete rows and columns from the table. Verify that you cannot modify the table. The Microsoft.Office.Tools.Word.GroupContentControl prevents you from making any modifications.

To test the customer feedback table

1. Click Choose your second building block to display the second building block gallery content control.

2. Click the drop-down arrow next to the Custom Gallery 1 heading in the control, and select Customer Table.

3. Click in the cell to the right of the Customer Name cell and type a name.

4. Click in the cell to the right of the Satisfaction Rating cell and select one of the available options.

Verify that you cannot type your own entry. The Microsoft.Office.Tools.Word.DropDownListContentControl allows users only to select from a list of entries.

5. Click in the cell to the right of the Comments cell and type some comments.

Optionally, add some content other than text, such as art or an embedded table. This is possible because the Microsoft.Office.Tools.Word.RichTextContentControl enables users to add content other than text.

6. Verify that you can add rows or columns to the table, and that you can delete rows and columns from the table. This is possible because you have not protected the table by putting it in a Microsoft.Office.Tools.Word.GroupContentControl.

7. Close the template.

-----
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.