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

VBA - The Word Working Partner - Part 3

By , About.com Guide

1 of 7

Using Toolbox Components

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.

~~~~~~~~~~~~~~~~~~~~~~~~~
(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
~~~~~~~~~~~~~~~~~~~~~~~~~

Explore Visual Basic
By Category
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB 6
  5. Learn VBA
  6. VBA - The Word Working Partner - Part 3

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

All rights reserved.