Adding controls to the Word document in a document-level project at design time.
Populating a text box when a button is clicked.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalize the IDE.
Prerequisites
You need the following components to complete this walkthrough:
Visual Studio opens the new Word document in the designer and adds the My Word Button project to Solution Explorer.
Add controls to the Word document
The user interface controls consist of a button and a text box on the Word document.
To add a button and a text box
Verify that the document is open in the Visual Studio designer.
From the Common Controls tab of the Toolbox, drag a TextBox control to the document.
Note
In Word, controls are dropped in-line with text by default. You can modify the way controls and shape objects are inserted by changing the default on the Edit tab of the Options dialog box in Word.
On the View menu, select Properties Window.
Find TextBox1 in the Properties window drop-down box and change the Name property of the text box to displayText.
Drag a Button control to the document and change the following properties.
Property
Value
Name
insertText
Text
Insert Text
Now you can write the code that will run when the button is clicked.
Populate the text box when the button is clicked
Every time the user selects the button, Hello World! is added to the text box.
To write to the text box when the button is clicked
In Solution Explorer, right-click ThisDocument, and then select View Code on the shortcut menu.
Add the following code to the Click event handler of the button.
Private Sub insertText_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles insertText.Click
Me.displayText.Text += "Hello World!"
End Sub