When you programmatically add a Windows Forms control to a document, you must provide the location of the control and a name that uniquely identifies the control. Visual Studio Tools for Office provides helper methods for each control. These methods are overloaded so that you can pass either a range or specific coordinates for the location of the control. For specific instructions, see How to: Add Windows Forms Controls to Office Documents.
When a document is saved and then closed, all dynamically created Windows Forms controls are removed from the document. You can add code to your solution to re-create the controls when the document is reopened. If you create dynamic Windows Forms controls by using an application-level add-in, the ActiveX wrappers for the controls are left in the document. For more information, see Persisting Dynamic Controls in Office Documents.
Note: |
|---|
Windows Forms controls cannot be programmatically added to protected documents. If you programmatically unprotect a Word document or Excel worksheet to add a control, you must write additional code to remove the control's ActiveX wrapper when the document is closed. The control's ActiveX wrapper is not automatically deleted from protected documents. |
Adding Custom Controls
If you want to add a System.Windows.Forms..::.Control that is not supported by the available helper methods (for example, a custom user control), use the following methods:
To add the control, pass the System.Windows.Forms..::.Control, a location for the control, and a name that uniquely identifies the control to the AddControl method. For Excel, this method returns an OLEObject. For Word, this method returns an OLEControl. These objects define how the control interacts with the worksheet or document.
The following code example demonstrates how to use the AddControl(Control, Range, String) method to dynamically add a custom user control to a worksheet. In this example, the user control is named UserControl1, and the Range is named range1. This example assumes that it is being run from one of the Sheetn classes in a document-level project for Excel.
Dim customControl As New UserControl1()
Dim dynamicControl As Microsoft.Office.Tools.Excel.OLEObject = _
Me.Controls.AddControl(customControl, range1, "dynamic")
UserControl1 customControl = new UserControl1();
Microsoft.Office.Tools.Excel.OLEObject dynamicControl =
this.Controls.AddControl(customControl, range1, "dynamic");
Using Members of Custom Controls
After using one of the AddControl methods to add a control to a worksheet or document, you now have two different control objects:
Many properties and methods are shared between these controls. It is important that you access these methods and properties through the appropriate control:
If you access a shared method or property from the System.Windows.Forms..::.Control, it might fail without warning or notification, or it can produce invalid results. Always use methods or properties of the OLEObject or OLEControl unless the method or property needed is not available; only then should you reference the System.Windows.Forms..::.Control.
For example, both the OLEObject class and the System.Windows.Forms..::.Control class have a Top property. To get or set the distance between the top of the control and the top of the document, use the Top property of the OLEObject, not the Top property of the System.Windows.Forms..::.Control.
' Property is set in relation to the document.
dynamicControl.Top = 100
' Property is set in relation to the container control.
customControl.Top = 100
// Property is set in relation to the document.
dynamicControl.Top = 100;
// Property is set in relation to the container control.
customControl.Top = 100;