| 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. |
|
You can add controls to your Microsoft Office Word 2003 document and Microsoft Office Excel 2003 workbook, and also remove them, at run time.
Persisting Controls in the Document
When you programmatically add a Windows Forms control, such as a ListBox or CheckBox to your Office solution, the control persists only while the document is open. When you close the document, the dynamically created Windows Forms controls are not persisted with the document. The next time the document is opened, the controls are recreated when the event containing the code is raised.
If you want to recreate the controls without relying on the user to raise the same event first, you can get the position of the underlying Shape or InlineShape of the control using the GetShapeForControl method or the GetInlineShapeForControl method. You can then use the position of the Shape or InlineShape to recreate the control. For more information on recreating controls programmatically, see Word Dynamic Controls Sample.
Host controls that are created at run time, such as a NamedRange, behave differently. When you close the document, any dynamically created host control disconnects its events and data binding functionality. Essentially, it becomes a native Office object. To reconnect the events and data bindings the next time the document is opened, you use the same method that you used to originally create the control, and pass in the same range and name of the original host control.
Note |
|---|
|
If you want to recreate a Microsoft.Office.Tools.Word.Bookmark control from an existing Microsoft.Office.Interop.Word.Bookmark, use the AddBookmark method and pass in the existing Microsoft.Office.Interop.Word.Bookmark. |
Controls Collection
When you programmatically add a control to a document, the control is added to the Controls collection which can be used to iterate through all of the controls in your document, including the controls you added at design time (static controls). You can remove dynamic controls from the collection by passing either the control or the name of the control to the Remove method. You cannot remove static controls at run time. Attempts to remove a static control will throw an exception.
Windows Forms Controls
Microsoft Visual Studio 2005 Tools for the Microsoft Office System provides a helper method for each of the common Windows Forms controls that can be hosted on a Word document or Excel worksheet. When you add a control programmatically, you must provide the location to place the control and a name that uniquely identifies the control. Each of the helper methods is overloaded so that you can either pass a range or specific coordinates for the location of the control.
Note |
|---|
|
When referencing or casting controls, be sure to use the correct control type. For example, if you are casting an object to a |
You can also programmatically add controls to your Word document or Excel worksheet by using the AddControl method. However, the helper methods are provided to remove the complexity of the AddControl method which should only be used to add custom controls to your document.
Note |
|---|
|
Windows Forms controls cannot be programmatically added to protected documents. If you programmatically unprotect the Word document or Excel worksheet in order 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. |
Helper Methods for Excel
Visual Studio Tools for Office provides a number of helper methods that can be used to programmatically add Windows Forms controls to Excel documents. Each method is overloaded so that you can pass in a Range or the coordinates of a specific location. When you create a control by passing in a Range, the control automatically resizes when cells within the range are resized.
The following is an alphabetic list of helper methods that can be used to programmatically add Windows Forms controls to Excel documents.
| Helper Method | Definition |
|---|---|
|
Adds a Button control to the specified range. | |
|
Adds a Button control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a Label control to the specified range. | |
|
Adds a Label control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a LinkLabel control to the specified range. | |
|
Adds a LinkLabel control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a TextBox control to the specified range. | |
|
Adds a TextBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a CheckBox control to the specified range. | |
|
Adds a CheckBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a RadioButton control to the specified range. | |
|
Adds a RadioButton control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a PictureBox control to the specified range. | |
|
Adds a PictureBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a CheckedListBox control to the specified range. | |
|
Adds a CheckedListBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a ComboBox control to the specified range. | |
|
Adds a ComboBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a ListView control to the specified range, setting the position and size of control. | |
|
Adds a ListView control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a TreeView control to the specified range. | |
|
Adds a TreeView control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a DateTimePicker control to the specified range. | |
|
Adds a DateTimePicker control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a MonthCalendar control to the specified range. | |
|
Adds a MonthCalendar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a DomainUpDown control to the specified range. | |
|
Adds a DomainUpDown control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a NumericUpDown control to the specified range. | |
|
Adds a NumericUpDown control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a TrackBar control to the specified range. | |
|
Adds a TrackBar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a RichTextBox control to the specified range. | |
|
Adds a RichTextBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a DataGridView control to the specified range. | |
|
Adds a DataGridView control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a HScrollBar control to the specified range. | |
|
Adds a HScrollBar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a VScrollBar control to the specified range. | |
|
Adds a VScrollBar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a PropertyGrid control to the specified range. | |
|
Adds a PropertyGrid control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a WebBrowser control to the specified range. | |
|
Adds a WebBrowser control at the exact position supplied rather than a range. The coordinates are represented in points. |
Helper Methods for Word
Visual Studio Tools for Office provides a number of helper methods that can be used to programmatically add Windows Forms controls to Word documents. Each method is overloaded so that you can pass in a Range or the coordinates of a specific location. When you create a control by passing in a Range, the control is added as an InlineShape, placed at the start of the range. You cannot change the Top and Left properties of controls that are added in line with text. When you create a control by passing in coordinates, the control is added as a floating Shape, placed at the location specified.
The following is an alphabetic list of helper methods that can be used to programmatically add Windows Forms controls to Word documents.
| Helper Method | Definition |
|---|---|
|
AddButton |
Adds a Button control at the start of the specified range. |
|
Adds a Button control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a Label control at the start of the specified range. | |
|
Adds a Label control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a LinkLabel control at the start of the specified range. | |
|
Adds a LinkLabel control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a TextBox control at the start of the specified range. | |
|
Adds a TextBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a CheckBox control at the start of the specified range. | |
|
Adds a CheckBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a RadioButton control at the start of the specified range. | |
|
Adds a RadioButton control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a PictureBox control at the start of the specified range. | |
|
Adds a PictureBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a CheckedListBox control at the start of the specified range. | |
|
Adds a CheckedListBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a ComboBox control at the start of the specified range. | |
|
Adds a ComboBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a ListView control to the specified range, setting the position and size of control. | |
|
Adds a ListView control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a TreeView control at the start of the specified range. | |
|
Adds a TreeView control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a DateTimePicker control at the start of the specified range. | |
|
Adds a DateTimePicker control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a MonthCalendar control at the start of the specified range. | |
|
Adds a MonthCalendar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a DomainUpDown control at the start of the specified range. | |
|
Adds a DomainUpDown control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a NumericUpDown control at the start of the specified range. | |
|
Adds a NumericUpDown control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a TrackBar control at the start of the specified range. | |
|
Adds a TrackBar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a RichTextBox control at the start of the specified range. | |
|
Adds a RichTextBox control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a DataGridView control at the start of the specified range. | |
|
Adds a DataGridView control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a HScrollBar control at the start of the specified range. | |
|
Adds a HScrollBar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a VScrollBar control at the start of the specified range. | |
|
Adds a VScrollBar control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a PropertyGrid control at the start of the specified range. | |
|
Adds a PropertyGrid control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a WebBrowser control at the start of the specified range. | |
|
Adds a WebBrowser control at the exact position supplied rather than a range. The coordinates are represented in points. |
Custom Controls
If you want to add a control that is not supported by the helper methods, you can use the generic AddControl method to programmatically add the control to the document. You pass the control, a location for the control and a name that uniquely identifies it to the AddControl method. The AddControl method returns an OLEObject in Excel and an OLEControl in Word. For example, the following code demonstrates how to dynamically add a custom user control to range1 of the worksheet.
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");
Because you now have two control references, one for the custom user control, and one for the OLEObject, many properties and methods are shared between each control. It is important that you access these methods and properties through the appropriate control.
| Properties and methods are members of both controls | Properties and methods are members of the custom control only |
|---|---|
|
Access the methods and properties through the OLEObject or OLEControl. |
Access the methods and properties through the custom control. |
If you access a shared method or property from the custom control, it will fail without warning or notification. For example, the Top property is a member of the OLEObject as well as the user control. Therefore, you must set the property on the OLEObject control and not on the user 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;
Setting the Top property on the user control will not produce valid results. A best practice is to always reference the OLEObject or OLEControl unless the method or property needed is not available; only then should you reference the custom control. The helper methods for adding the most common Windows Forms controls are provided to take care of this complexity for you.
Host Controls
Host controls can also be added and removed at runtime. Visual Studio Tools for Office provides a helper method for each of the common host controls that can be hosted on a Word document or Excel worksheet. When you add a control programmatically, you must provide the location to place the control and a name that uniquely identifies the control.
Helper Methods for Excel
The following is an alphabetic list of helper methods that can be used to programmatically add host controls to Office Excel documents.
| Helper Method | Definition |
|---|---|
|
Adds a Chart control to the specified range. | |
|
Adds a Chart control at the exact position supplied rather than a range. The coordinates are represented in points. | |
|
Adds a ListObject control to the specified range. | |
|
Adds a NamedRange control to the specified range. |
Helper Methods for Word
The following is the helper method that can be used to programmatically add host controls to Office Word documents.
| Helper Method | Definition |
|---|---|
|
Adds a Bookmark control at the specified range. |
See Also
Tasks
How to: Add Controls to Office DocumentsHow to: Resize Controls Within Worksheet Cells
Reference
Microsoft.Office.Tools.Excel.ControlCollection.AddControl(System.Windows.Forms.Control,Microsoft.Office.Interop.Excel.Range,System.String)Microsoft.Office.Tools.Word.ControlCollection.AddControl(System.Windows.Forms.Control,Microsoft.Office.Interop.Word.Range,System.Single,System.Single,System.String)
Microsoft.Office.Tools.Excel.ControlCollection.Remove(System.String)
Microsoft.Office.Tools.Word.ControlCollection.Remove(System.String)
Concepts
Windows Forms Controls on Office Documents OverviewOther Resources
Controls in Microsoft Office ProjectsExcel Host Controls
Word Host Controls
Note