Megosztás a következőn keresztül:


How to: Add Windows Forms controls to Office documents

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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:

Add controls at design time

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

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

    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.

    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.

    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.

    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.

    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.

Add controls at run time in document-level projects

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 Add 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
    
    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";
    }
    

Add controls at run time in VSTO Add-ins

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 Controls property of the new host item. In Excel, use methods of the 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 Add controls to Office documents at run time.

For more information about generating host items in VSTO Add-in projects, see Extend 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).

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

See also