Excel Add-In Dynamic Controls Sample

Note

This sample runs only in Microsoft Office Excel 2007.

When you do not know at design time how many controls will be required in a document at run time, you can add controls dynamically in response to events.

This sample demonstrates how to add and remove Windows Forms controls and host controls at run time.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To run this sample

  1. Press F5.

  2. Click a worksheet cell.

    To add a ListObject control, select one or more cells.

  3. Select a check box in the task pane.

    A control is added at the selected cell.

  4. Clear the check box that you selected.

    The control is removed from the document.

Controls created by add-ins are not saved automatically in the workbook. This sample saves the control information to a custom XML part.

To save the controls

  1. Press F5.

  2. Click a worksheet cell.

    To add a ListObject control, select one or more cells.

  3. Select a check box in the task pane.

    A control is added at the selected cell.

  4. Click the Save button.

    The Delete controls dialog box appears.

  5. Click No.

  6. In the Save As dialog box, type a file name for this worksheet, and click Save.

  7. Exit Excel.

  8. Open the file that you just saved.

    The controls are still present in your workbook.

Demonstrates

  • Dynamically placing Windows Forms controls and host controls on an Excel worksheet.

  • Dynamically removing Windows Forms controls and host controls from an Excel worksheet.

  • Storing host control information in an XML part that is saved as part of the document.

See Also

Tasks

Word Add-In Dynamic Controls Sample

Concepts

Host Items and Host Controls Overview

Actions Pane Overview

Other Resources

Document-Level Samples

Application-Level Samples

Controls on Office Documents

Change History

Date

History

Reason

July 2008

Added topic.

SP1 feature change.