Export (0) Print
Expand All

How to: Add Controls to Office Documents

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

A control is a component on the document used to display information or accept user input. You can add Windows Forms controls to Microsoft Office Excel 2003 and Microsoft Office Word 2003 documents at design time or at run time. For example, you can add a ComboBox control to your worksheet so that users can select from a list of options. You can also add host controls, such as Bookmark controls and NamedRange controls, to Office documents. For more information, see Word Host Controls and Excel Host Controls.

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.

Adding Controls at Design Time

To drag a Windows Forms control to the document

  1. Create or open an Excel Workbook project or Word Document project in Visual Studio so that the document is visible in the designer. For information on creating projects, see How to: Create Visual Studio Tools for Office Projects.

  2. In the Common Controls tab of the Toolbox, click the control you want to add, and drag it to the document.

    NoteNote

    When you select a control in Excel, you will see =EMBED("WinForms.Control.Host","") in the Formula Bar. This text is necessary and should not be deleted.

To draw a Windows Forms control on the document

  1. Create or open an Excel Workbook project or Word Document project in Visual Studio so that the document is visible in the designer. For information on creating projects, see How to: Create Visual Studio Tools for Office Projects.

  2. In the Common Controls tab of the Toolbox, click the control you want to add.

  3. On the document, click where you want the upper-left corner of the control to be located, and drag to where you want the lower-right corner of the control to be located.

    The control is added to the document with the specified location and size.

    NoteNote

    When you select a control in Excel, you will see =EMBED("WinForms.Control.Host","") in the Formula Bar. This text is necessary and should not be deleted.

To add a Windows Forms control to the document by single-clicking the control

  1. Create or open an Excel Workbook project or Word Document project in Visual Studio so that the document is visible in the designer. For information on creating projects, see How to: Create Visual Studio Tools for Office Projects.

  2. In the Common Controls tab of the Toolbox, click the control you want to add

  3. One the document, click where you want the control to be added.

    The control is added to the document with the default size.

    NoteNote

    When you select a control in Excel, you will see =EMBED("WinForms.Control.Host","") in the Formula Bar. This text is necessary and should not be deleted.

To add a Windows Forms control to the document by double-clicking the control

  1. Create or open an Excel Workbook project or Word Document project in Visual Studio so that the document is visible in the designer. For information on creating projects, see How to: Create Visual Studio Tools for Office Projects.

  2. In the Common Controls tab of the Toolbox, double-click the control you want to add.

    The control is added to the document at the center of the document or active pane.

    NoteNote

    When you select a control in Excel, you will see =EMBED("WinForms.Control.Host","") in the Formula Bar. This text is necessary and should not be deleted.

To add a Windows Forms control to the document by pressing the ENTER key

  1. Create or open an Excel Workbook project or Word Document project in Visual Studio so that the document is visible in the designer. For information on creating projects, see How to: Create Visual Studio Tools for Office Projects.

  2. In the Common Controls tab of the Toolbox, click the control you want to add, and press the ENTER key.

    The control is added to the document at the center of the document or active pane.

    NoteNote

    When you select a control in Excel, you will see =EMBED("WinForms.Control.Host","") in the Formula Bar. This text is necessary and should not be deleted.

You can also add Windows Forms controls dynamically to a document at run time using the helper methods provided by Microsoft Visual Studio 2005 Tools for the Microsoft Office System. In the example below, a Button is added to cell A1 of an Excel Application project. For information on adding other Windows Forms controls, see Adding Controls to Office Documents at Run Time.

Adding Controls at Run Time

To add a Windows Forms control programmatically

  • In the Startup event handler of ThisWorkbook, insert the following code to add a Button to cell C5:

    private void Sheet1_Startup(object sender, System.EventArgs e)
    {
        Microsoft.Office.Tools.Excel.Controls.Button salesButton;
        salesButton = this.Controls.AddButton(this.get_Range("C5", missing), "salesButton");
        salesButton.Text = "Calculate Total Sales";
    }
    
    

See Also

Community Additions

ADD
Show:
© 2015 Microsoft