Windows Forms Controls on Office Documents Overview
This documentation is archived and is not being maintained.

Windows Forms Controls on Office Documents Overview

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

Windows Forms controls are objects that users can interact with to enter or manipulate data. In addition to adding Windows Forms controls to Windows Forms, you can add them to Microsoft Office Word 2003 documents, Microsoft Office Excel 2003 worksheets, and actions panes. For more information on actions panes, see Actions Pane Overview.

Using Windows Forms Controls

Determining whether to add Windows Forms controls to your document, the actions pane or a Windows form depends on several factors. When designing the user interface (UI) of your application, consider the use of Windows Forms Controls in the following table.

On the document
  • When you want to display the controls 100% of the time.

  • When you want users to enter data directly in the document. For example, forms-based documents where the editing surface is locked.

  • When you want the controls to display in line with the data in the document. For example, if you are adding buttons to each row of a list object, you would want them in line with each list item.

On the actions pane
  • When you want to provide contextual information to the user.

  • When you want only the results to appear in the document, as opposed to an entire list of data.

  • When you want to ensure that the controls do not get printed with the document.

  • When you want to ensure that the view of the document is not obstructed by the UI.

On a Windows form
  • When you want to control the size of UI.

  • When you want to prevent the users from hiding or deleting the controls.

  • When input from the user is required.

Creating Custom User Controls

You can add a user control to your project and then add it to the toolbox. You can then drag the user control directly to your document in the same way you would add a Windows Forms control to your document. There are some things to keep in mind when creating user controls for use on your document:

  • Microsoft Visual Studio 2005 Tools for the Microsoft Office System generates a wrapper class derived from the user control to extend it and support its use on the document. Because you cannot derive from a sealed class, do not create a sealed user control.

  • User controls must have the ComVisibleAttribute attribute set to true in order to be used in a Visual Studio Tools for Office solution. User controls created inside a Visual Studio Tools for Office project have this attribute set to true by default, but user controls that are part of outside projects might not have this attribute set to true.

  • After you have added a user control to the document, do not rename or delete the UserControl class from the project. If you need to rename the user control you must first delete it from the document, and then add it again after the name has been changed.

Adding Controls at Design Time

You can add Windows Forms controls to your Word and Excel documents in the same manner that you add controls to Windows Forms. You can drag a control from the Windows Forms tab of the Toolbox directly to your document.

When you add a control to a Word document, the control is added in line with text. To change the style of the control, click the Format menu, click Control, and then select a wrapping style in the Layout tab of the Format Object dialog box. If you open a Word template in the Visual Studio designer, non-inline controls on the template might not be visible because Visual Studio opens the template in Normal view. To view the controls, click the View menu, point to Microsoft Office Word View, and click Print Layout.

In Excel, you can set the control to resize with the cell. For more information, see How to: Resize Controls Within Worksheet Cells.

NoteNote

Windows Forms controls are not supported inside of a header or footer, or within a subdocument.

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.

Arranging Controls on the Document

If you add multiple controls to your Word and Excel documents, you can quickly set the alignment of all of the selected controls by using the Microsoft Word or Microsoft Excel toolbars.

When a Word document is open in Visual Studio, the Microsoft Word toolbar is available within Visual Studio. The Microsoft Excel toolbar is available when you open an Excel worksheet in Visual Studio. Each of these toolbars has a number of buttons that enable you to arrange multiple controls on the document, including:

  • Align Lefts

  • Align Centers

  • Align Rights

  • Align Tops

  • Align Middles

  • Align Bottoms

  • Make Horizontal Spacing Equal

  • Make Vertical Spacing Equal

Displaying Windows Forms Controls on Documents

When an Excel workbook that contains Windows Forms controls is opened, the workbook displays a bitmap of the control in an ActiveX control until the customization code runs and loads the actual control. Excel creates this bitmap and stores it in the workbook whenever the workbook is saved. The bitmap shows the control as it appeared the last time the workbook was saved, including any data that the control was displaying. For more information about the ActiveX control that contains Windows Forms controls and bitmaps, see Limitations of Windows Forms Controls on Office Documents.

In certain conditions, the code does not load and only the bitmap is displayed, such as when the user opens the workbook in design mode. Also, if the user opens the workbook on a computer that does not have the Visual Studio Tools for Office runtime installed, the customization cannot run to load the controls and therefore only the bitmap of the control is visible. You should always remove personal information from controls on workbooks before saving the workbook and sending it to another user to ensure that your personal information is not accidentally disclosed.

Adding and Removing Controls Programmatically

You can add and remove Windows Forms controls on your Word and Excel document at run time. Visual Studio Tools for Office provides helper methods for the common Windows Forms controls. These helper methods enable you to quickly add controls to your Office document and access the combined Windows Forms control functionality and Office-related functionality of these controls. If you want to add a control that is not supported by the various helper methods provided, you can use the AddControl method. Fore more information on programmatically adding and removing controls, see Adding Controls to Office Documents at Run Time. You can remove controls from the document by using the Remove method of the ControlsCollection or by calling the Delete method of the control.

NoteNote

Windows Forms controls are not supported inside of a header or footer, or within a subdocument.

Adding Components

Certain controls or components are not visible on the document and are instead displayed in a component tray. Visual Studio Tools for Office provides a component tray for each document window. The component tray will only display on the screen if components exist on the document. In an Excel project, you can drag components that you want to share among all worksheets, such as a DataSet, onto the workbook designer.

Understanding the Differences in Windows Forms Controls on Office Documents

Windows Forms Controls generally have the same behavior on an Office document as on a Windows Form or an actions pane, but some differences do exist. For information on the differences and limitations of Windows Forms controls on Office documents, see Limitations of Windows Forms Controls on Office Documents.

See Also

Show:
© 2016 Microsoft