How to: Add Windows Forms Controls to Office Documents

 

You can add Windows Forms controls to Microsoft Office Excel and Microsoft Office Word documents at design time in document-level projects. At run time, you can add controls in document-level customizations and in VSTO Add-ins. For example, you can add a ComboBox control to your worksheet so that users can select from a list of options.

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

This topic describes the following tasks:

link to video For a related video demonstration, see How Do I: Add Controls to a Document Surface at Runtime?.

There are several ways to add Windows Forms controls to the document in a document-level project at design time.

System_CAPS_ICON_note.jpg 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 Personalizing the Visual Studio IDE.

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 Office Projects in Visual Studio.

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

    System_CAPS_ICON_note.jpg Note


    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 Office Projects in Visual Studio.

  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.

    System_CAPS_ICON_note.jpg Note


    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 Office Projects in Visual Studio.

  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.

    System_CAPS_ICON_note.jpg Note


    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 Office Projects in Visual Studio.

  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.

    System_CAPS_ICON_note.jpg Note


    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 Office Projects in Visual Studio.

  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.

    System_CAPS_ICON_note.jpg Note


    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 programmatically add Windows Forms controls to a document at run time. In Word, use methods of the Controls property of the ThisDocument class. In Excel, use methods of the Controls property of a Sheetn class. Each method has several overloads that enable you to specify the location of the control in different ways.

When you add a Windows Forms control to a document at run time, the control is not persisted in the document when the document is closed. You can recreate the control the next time the document is opened. For more information, see Adding Controls to Office Documents at Run Time.

To add a Windows Forms control at run time

  1. Use a method that has the name Add<control class> (where control class is the class name of the Windows Forms control that you want to add, such as AddButton).

    The following code example demonstrates how to add a Button to cell C5 of Sheet1 in a document-level project for Excel.

        Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles Me.Startup
    
            Dim salesButton As Microsoft.Office.Tools.Excel.Controls.Button
            salesButton = Me.Controls.AddButton(Me.Range("C5"), "salesButton")
            salesButton.Text = "Calculate Total Sales"
        End Sub
    

You can add Windows Forms controls programmatically to any open document at run time. First, generate a host item that is based on an open document or worksheet. Then, in Word, use methods of the Document.Controls property of the new host item. In Excel, use methods of the Worksheet.Controls property of the new host item. Each method has several overloads that enable you to specify the location of the control in different ways.

When you add a Windows Forms control to a document at run time, the control is not persisted in the document when the document is closed. You can recreate the control the next time the document is opened. For more information, see Adding Controls to Office Documents at Run Time.

For more information about generating host items in VSTO Add-in projects, see Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time.

To add a Windows Forms control at run time

  1. Use a method that has the name Add<control class> (where control class is the class name of the Windows Forms control that you want to add, such as AddButton).

    System_CAPS_ICON_note.jpg Note


    In VSTO Add-in projects that target the .NET Framework 4 or later, you must add a reference to the Microsoft.Office.Tools.Excel.v4.0.Utilities.dll or Microsoft.Office.Tools.Word.v4.0.Utilities.dll assembly before you can access the Add<control class> methods.

    The following code example demonstrates how to add a Button to the first paragraph of the active document by using a Word VSTO Add-in.

            Dim salesButton As Microsoft.Office.Tools.Word.Controls.Button
    
            Dim extendedDocument As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    
            extendedDocument.Paragraphs(1).Range.InsertParagraphBefore()
            salesButton = extendedDocument.Controls.AddButton( _
                extendedDocument.Paragraphs(1).Range, 75, 18, "salesButton")
            salesButton.Text = "Calculate Sales"
    

Windows Forms Controls on Office Documents Overview
Adding Controls to Office Documents at Run Time
How to: Resize Controls Within Worksheet Cells
Host Items and Host Controls Overview
Optional Parameters in Office Solutions

Show: