1. Home
  2. Computing & Technology
  3. Visual Basic
VBA - The Word Working Partner
Using Toolbox Components
 More of this Feature
• Part 1: Using VBA with Microsoft Word
• Part 2: Automate Your Letter Writing
• Part 3: The AboutVBA.DOC Code Sample
• Part 4: Word Objects
• Part 5: The Automated Tutorial Review Program
 
 Join the Discussion
Create a brilliant VBA app?
Tell Us About It!
 
 Related Resources
• Segment One of the VBA Tutorial
• The About Visual Basic Subjects List
• Beginning Visual Basic
• Visual Basic 6
 
 From Other Guides
• About Desktop Publishing for MS Word
• About Business Software
 
 Elsewhere on the Web
• MS: Word Objects
• MS: Templates vs Add-In's
 

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:

Private Sub myCleverRemarks_Click()
Selection.Text = myCleverRemarks.Value
frmRemarks.Hide
End Sub

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:

All VBA Programs

Download the document (includes all of the VBA) and try it yourself!

Next page > Using Toolbox Components > Page 1, 2, 3, 4, 5, 6
Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.