Walkthrough: Changing Document Formatting Using CheckBox Controls

This walkthrough demonstrates how to use Windows Forms controls in a document-level customization for Microsoft Office Word to change text formatting.

Applies to: The information in this topic applies to document-level projects for Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.

This walkthrough illustrates the following tasks:

  • Adding text and a control to the document in a document-level project at design time.

  • Formatting the text when an option is selected.

To see the result as a completed sample, see the Word Controls Sample at Office Development Samples and Walkthroughs.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

Prerequisites

You need the following components to complete this walkthrough:

-

An edition of Visual Studio 2010 that includes the Microsoft Office developer tools. For more information, see [Configuring a Computer to Develop Office Solutions](bb398242\(v=vs.100\).md).
  • Word 2007 or Word 2010.

Creating the Project

The first step is to create a Word Document project.

To create a new project

  • Create a Word Document project with the name My Word Formatting. In the wizard, select Create a new document.

    For more information, see How to: Create Office Projects in Visual Studio.

    Visual Studio opens the new Word document in the designer and adds the My Word Formatting project to Solution Explorer.

Adding Text and Controls to the Word Document

For this walkthrough, add three check boxes and some text in a Bookmark control to the Word document. The check boxes will present options to the user for formatting the text.

To add three check boxes

  1. Verify that the document is open in the Visual Studio designer.

  2. From the Common Controls tab of the Toolbox, drag the first CheckBox control to the document.

  3. In the Properties window, change the following properties.

    Property

    Value

    Name

    applyBoldFont

    Text

    Bold

  4. Press Enter to move the insertion point below the first check box.

  5. Add a second check box to the document below the ApplyBoldFont check box and change the following properties.

    Property

    Value

    Name

    applyItalicFont

    Text

    Italic

  6. Press Enter to move the insertion point below the second check box.

  7. Add a third check box to the document below the ApplyItalicFont check box and change the following properties.

    Property

    Value

    Name

    applyUnderlineFont

    Text

    Underline

To add text and a Bookmark control

  1. Move the insertion point below the check box controls and type the following text:

    Click a check box to change the formatting of this text.

  2. From the Word Controls tab of the Toolbox, drag a Bookmark control to the document.

    The Add Bookmark Control dialog box appears.

  3. Select the text you added to the document and click OK.

    A Bookmark control named Bookmark1 is added to the selected text in the document.

  4. In the Properties window, change the value of the (Name) property to fontText**.**

Next, write the code to format the text when a check box is checked or cleared.

Formatting the Text When a Check box is Checked or Cleared

When the user selects a formatting option, change the format of the text in the document.

To change formatting when a check box is selected

  1. Right-click ThisDocument in Solution Explorer, and then click View Code on the shortcut menu.

  2. For C# only, add the following constants to the ThisDocument class.

    const int WordTrue = -1;
    const int WordFalse = 0;
    
  3. Add the following code to the Click event handler of the applyBoldFont check box.

    Private Sub applyBoldFont_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles applyBoldFont.Click
    
        Me.fontText.Bold = Me.applyBoldFont.Checked
    End Sub
    
    private void applyBoldFont_Click(object sender, System.EventArgs e)
    {
        if (this.applyBoldFont.Checked == true)
        {
            this.fontText.Bold = WordTrue;
        }
        else
        {
            this.fontText.Bold = WordFalse;
        }
    }
    
  4. Add the following code to the Click event handler of the applyItalicFont check box.

    Private Sub applyItalicFont_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles applyItalicFont.Click
    
        Me.fontText.Italic = Me.applyItalicFont.Checked
    End Sub
    
    private void applyItalicFont_Click(object sender, System.EventArgs e)
    {
        if (this.applyItalicFont.Checked == true)
        {
            this.fontText.Italic = WordTrue;
        }
        else
        {
            this.fontText.Italic = WordFalse;
        }
    }
    
  5. Add the following code to the Click event handler of the applyUnderlineFont check box.

    Private Sub applyUnderlineFont_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles applyUnderlineFont.Click
    
        If Me.applyUnderlineFont.Checked Then
            Me.fontText.Underline = Word.WdUnderline.wdUnderlineSingle
        Else
            Me.fontText.Underline = Word.WdUnderline.wdUnderlineNone
        End If
    End Sub
    
    private void applyUnderlineFont_Click(object sender, System.EventArgs e)
    {
        if (this.applyUnderlineFont.Checked == true)
        {
            this.fontText.Underline = Word.WdUnderline.wdUnderlineSingle;
        }
        else
        {
            this.fontText.Underline = Word.WdUnderline.wdUnderlineNone;
        }
    }
    
  6. In C#, you must add event handlers for the text boxes to the Startup event. For information about how to create event handlers, see How to: Create Event Handlers in Office Projects.

    this.applyBoldFont.Click += new EventHandler(applyBoldFont_Click);
    this.applyItalicFont.Click += new EventHandler(applyItalicFont_Click);
    this.applyUnderlineFont.Click += new EventHandler(applyUnderlineFont_Click);
    

Testing the Application

You can now test your document to verify that the text is formatted correctly when you select or clear a check box.

To test your document

  1. Press F5 to run your project.

  2. Select or clear a check box.

  3. Confirm that the text is formatted correctly.

Next Steps

This walkthrough shows the basics of using check boxes and programmatically changing text formatting on Word documents. Here are some tasks that might come next:

See Also

Concepts

Walkthroughs Using Word

NamedRange Control

Limitations of Windows Forms Controls on Office Documents

Other Resources

Office Development Samples and Walkthroughs