Document.Controls Property (2007 System)

Gets the collection of managed controls that are contained on the document.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word.v9.0 (in Microsoft.Office.Tools.Word.v9.0.dll)

Syntax

'Declaration
<BrowsableAttribute(False)> _
Public Overridable ReadOnly Property Controls As ControlCollection
'Usage
Dim instance As Document 
Dim value As ControlCollection 

value = instance.Controls
[BrowsableAttribute(false)]
public virtual ControlCollection Controls { get; }
[BrowsableAttribute(false)]
public:
virtual property ControlCollection^ Controls {
    ControlCollection^ get ();
}
public function get Controls () : ControlCollection

Property Value

Type: Microsoft.Office.Tools.Word.ControlCollection
A ControlCollection that contains all the managed controls on the document.

Remarks

A document in a Visual Studio Tools for Office solution can contain Windows Forms controls and host controls. For more information, see Controls on Office Documents.

To manage the controls on a document at run time, use the methods and properties of the ControlCollection object returned by the Controls property. For more information, see Adding Controls to Office Documents at Run Time.

Examples

The following code example adds text to the first paragraph and creates a Bookmark control that encompasses the paragraph. The code then shows a message box that displays the total number of controls on the document.

This version is for a document-level customization.

Private Sub DocumentControls()
    Me.Paragraphs(1).Range.InsertParagraphAfter()
    Me.Paragraphs(1).Range.Text = "This is some sample text." 
    Me.Controls.AddBookmark(Me.Paragraphs(1).Range, "Bookmark1")
    MessageBox.Show("Total number of controls: " & Me.Controls.Count.ToString())
End Sub
private void DocumentControls()
{
    this.Paragraphs[1].Range.InsertParagraphAfter();
    this.Paragraphs[1].Range.Text = "This is some sample text.";
    this.Controls.AddBookmark(this.Paragraphs[1].Range, "Bookmark1");
    MessageBox.Show("Total number of controls: " +
        this.Controls.Count.ToString());
}

This version is for an application-level add-in.

Private Sub DocumentControls()
    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    vstoDoc.Paragraphs(1).Range.InsertParagraphAfter()
    vstoDoc.Paragraphs(1).Range.Text = "This is some sample text."
    vstoDoc.Controls.AddBookmark(vstoDoc.Paragraphs(1).Range, "Bookmark1")
    System.Windows.Forms.MessageBox.Show("Total number of controls: " & _
                                         vstoDoc.Controls.Count.ToString())
End Sub
private void DocumentControls()
{
    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    vstoDoc.Paragraphs[1].Range.InsertParagraphAfter();
    vstoDoc.Paragraphs[1].Range.Text = "This is some sample text.";
    vstoDoc.Controls.AddBookmark(vstoDoc.Paragraphs[1].Range, "Bookmark1");
    System.Windows.Forms.MessageBox.Show("Total number of controls: " +
        vstoDoc.Controls.Count.ToString());
}

.NET Framework Security

See Also

Reference

Document Class

Document Members

Microsoft.Office.Tools.Word Namespace

Other Resources

Controls on Office Documents

Adding Controls to Office Documents at Run Time

Change History

Date

History

Reason

July 2008

Added a version of the code example for an application-level add-in.

SP1 feature change.