Using ActiveX Controls on Word Documents
You can add controls to your documents to create interactive documents, such as online forms. For general information about adding and working with controls, see Using ActiveX controls on a document and Creating a custom dialog box .
Keep the following points in mind when you're working with controls on documents:
- You can add ActiveX controls to the document's text layer or drawing layer. To add a control to the drawing layer, click the control on the Control Toolbox. To add a control to the text layer, hold down the SHIFT key while you click a control on the Control Toolbox.
- A control you add to the text layer is an InlineShape object to which you gain access programmatically through the InlineShapes collection. A control you add to the drawing layer is a Shape object to which you gain access programmatically through the Shapes collection.
- Controls in the text layer are treated like characters and are positioned as characters within a line of text.
- In design mode, ActiveX controls in the drawing layer are visible only in print layout view or web layout view.
- If you want the user to use the ActiveX controls but not change the document's layout, protect the document by clicking the Protect Form button on the Forms toolbar.
- Word implements the LostFocus and GotFocus events for ActiveX controls on a document. The other events listed in the Procedure drop-down list box are documented in Microsoft Forms Help (Fm20.chm). For more information about using events with ActiveX controls see the Control and dialog box events and the Using events with ActiveX controls topics.
- If you want to add form fields instead of ActiveX controls to your document to create an online form, use the Forms toolbar.
- The Me keyword in an event procedure for an ActiveX control on a document refers to the document, not to the control.
Writing event code for controls on documents is very similar to writing event code for controls on forms. The following SpinUp and SpinDown event procedures change the value of the TextBox control named "TextBox1" on the document where the SpinButton control named "SpinButton1" resides. The text box value is decreased by one when the user clicks the lower or left spin-button arrow and incremented by one when the user clicks the upper or right spin-button arrow.
Private Sub SpinButton1_SpinDown()
Me.TextBox1.Value = Me.TextBox1.Value - 1
End Sub
FakePre-f42a18958df3483ba97879a67fc26022-7cddbe85a8a04ad58d403a5587c1d158
The following Click event procedure switches to print view and sets the magnification to 100 percent for the document where the command button named "cmdChangeView" resides.
Private Sub cmdChangeView_Click()
With Me.ActiveWindow.View
.Type = wdPrintView
.Zoom.Percentage = 100
End With
End Sub
See Also | Adding Controls to a Document | Adding Controls to a UserForm | Creating a Custom Dialog Box | Creating a UserForm | Displaying a Custom Dialog Box | Initializing Control Properties | Setting Control Properties | Using ActiveX Controls on a Document | Using Control Values While Code is Running