| VBA - The Word Working Partner | |||||||||||||||||||||||
| Using Toolbox Components | |||||||||||||||||||||||
The main thing we haven't done yet is create a program that uses components from the Toolbox. To do this, we're going to create a form that allows us to pick a comment from a combobox component. We don't want this form to display on top of our Word document all the time, so we're also going to code a short procedure to display the form that we will attach to yet another button on the toolbar. First, add a form to your project by selecting Userform under the Insert menu of the Visual Basic Editor in Word. Click the form to display it and make sure Toolbox is selected under the View menu. Click the Combobox control and draw it on the form just as you would in VB 6. Switch to the Code view under the View menu and enter this code:
Notice that I changed some properties as well. I changed the name of the Combobox to MyCleverRemarks and the name of the Form to frmRemarks. Feel free to experiment! You can download the document with the completed VBA code if you get confused. To display the Form and populate the Combobox, I created this program and attached it to a button on the toolbar. I also added a Public Boolean variable First to the front of the module and set the value of the variable to True when in the Open event of the Form. This prevents the Combobox from being loaded more than once if subroutine is called multiple times. (In the Document code module)
Private Sub Document_Open()
First = True
End Sub
(In the NewMacros code module)
Public First As Boolean
Sub ShowRemarks()
If First = True Then
frmRemarks.myCleverRemarks.AddItem ("What, me worry?")
frmRemarks.myCleverRemarks.AddItem ("Who voted for this guy?")
frmRemarks.myCleverRemarks.AddItem ("Caveat emptor!")
frmRemarks.myCleverRemarks.ListIndex = 0
End If
frmRemarks.Show
First = False
End Sub
This pretty much completes the document. Here's a complete automated review of the tutorial accomplished by clicking buttons and entering just one number:
Download the document (includes all of the VBA) and try it yourself! Next page > Using Toolbox Components > Page 1, 2, 3, 4, 5, 6 |
|||||||||||||||||||||||


