Partager via


Document.Controls Property

Definition

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

public:
 property Microsoft::Office::Tools::Word::ControlCollection ^ Controls { Microsoft::Office::Tools::Word::ControlCollection ^ get(); };
public Microsoft.Office.Tools.Word.ControlCollection Controls { get; }
member this.Controls : Microsoft.Office.Tools.Word.ControlCollection
Public ReadOnly Property Controls As ControlCollection

Property Value

A ControlCollection that contains all the managed controls on the document.

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 example is for an application-level add-in.

private void DocumentControls()
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    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());
}
Private Sub DocumentControls()
    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    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

Remarks

A document in an 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.

Applies to