Export (0) Print
Expand All

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 application-level 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 application-level projects for the following applications: Excel 2013 and Excel 2010; Word 2013 and Word 2010. 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.

NoteNote

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 Customizing Development Settings.

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.

    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 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.

    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 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.

    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 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.

    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 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.

    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 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

  • 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 void Sheet1_Startup(object sender, System.EventArgs e)
    {
        Microsoft.Office.Tools.Excel.Controls.Button salesButton;
        salesButton = this.Controls.AddButton(this.get_Range("C5"), "salesButton");
        salesButton.Text = "Calculate Total Sales";
    }
    

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 application-level projects, see Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.

To add a Windows Forms control at run time

  • 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()).

    Note Note

    In application-level add-in projects that target the .NET Framework 4 or the .NET Framework 4.5, 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 add-in.

    Microsoft.Office.Tools.Word.Controls.Button salesButton;
    
    Document extendedDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    
    
    extendedDocument.Paragraphs[1].Range.InsertParagraphBefore();
    salesButton = extendedDocument.Controls.AddButton(
        extendedDocument.Paragraphs[1].Range, 75, 18, "salesButton");
    salesButton.Text = "Calculate Sales";
    
Show:
© 2014 Microsoft