This documentation is archived and is not being maintained.

Walkthrough: Changing Document Formatting Using CheckBox Controls

Note Required applications

The features in this topic are available only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005

    -or-

    Visual Studio Team System

  • Microsoft Office 2003

This walkthrough demonstrates the basics of using controls on a Microsoft Office Word 2003 document to change text formatting. You will use Microsoft Visual Studio 2005 Tools for the Microsoft Office System to create and add code to your project. To see the result as a completed sample, see Word Controls Sample.

During this walkthrough, you will learn how to:

  • Add text and a control to a document.

  • Format the text when an option is selected.

  • Test your project.

NoteNote

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, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To complete this walkthrough, you will need:

  • Visual Studio Tools for Office.

  • Microsoft Office Word 2003.

In this procedure, you will create a Word Document project using Visual Studio Tools for Office.

To create a new project

  • Create a Word Document project with the name My Word Formatting. Make sure that Create a new document is selected. For more information, see How to: Create Visual Studio Tools for Office Projects.

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

For this walkthrough, you will need three check boxes and some text in a Bookmark control on the Word document.

To add three check boxes

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

  2. From Common Controls tab of the Toolbox, drag a CheckBox and drag it to the document.

  3. From the View menu, select Properties Window.

  4. Be sure that Checkbox1 is visible in the object name list box of the Properties window, and change the following properties:

    Property Value

    Name

    applyBoldFont

    Text

    Bold

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

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

    Property Value

    Name

    applyItalicFont

    Text

    Italic

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

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

    Property Value

    Name

    applyUnderlineFont

    Text

    Underline

    Next, you will drag the Bookmark control to the document.

    NoteNote

    You can also add the Bookmark control using the Bookmark dialog box, found in the Insert menu.

To add Text and a Bookmark control

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

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

  2. From the Word Controls tab in 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. Be sure that Bookmark1 is visible in the object name list box of the Properties window, and change the name property to fontText.

Next, write the code to format 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 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 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 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 as shown below. For information on creating event handlers, see How to: Create Event Handlers in Visual Studio Tools for Office.

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

You can now test your document to make sure 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.

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:

Show: