Getting Started Using VBA: The Word Working Partner

106 31
< Continued from page 2

For the Word program, lets use an "Automated Rating Description System" that substitutes a description for a numeric rating for this course in Word.

To create this system, add the text to the document as shown below.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Look for a "Developer" tab in Word 2007. It's not turned on by default so if it's not visible, click the Microsoft Office Button, and then click Word Options.


Click Popular, and select the Show Developer tab in the Ribbon check box. That should add a new "Developer" tab to your system. Click that tab to bring up the Visual Basic editor and enter the code for the VBA macro.

In the editor, Click the Design icon near the top to toggle into design mode. This "turns on" several features in Word and "turns off" others. For example, if you click a Button in design mode, the Button is selected. Out of design mode, clicking a Button will execute the Button Click event subroutine.

Click the down arrow beside the Legacy Tools icon to display the controls toolbox. Click the Button control to add it to your document. Remember that controls are added "in line" with your text so position the insertion point where you want the Button to be before adding it. Click Properties to change the Name and Caption properties of the Button.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

You can add the event subroutine that is called when the Button is clicked automatically by double clicking the Button.

This opens the Visual Basic code editor window and fills in an initial shell of the subroutine all at once. The completed code in the shell is shown below.

Private Sub RateCourse_Click()
      If ActiveDocument.Bookmarks.Exists("Rating") = True Then
            ActiveDocument.Bookmarks("Rating").Select
            theRating = Selection.Text
            Selection.MoveRight _
                  Unit:=wdCharacter, Count:=1, Extend:=wdExtend
            Selection.TypeText ""
      Else
            MsgBox ("Rating system bookmark missing!")
      End If
      Select Case theRating
      Case "1"
            Selection.Text = _
            "Huzza! Great! Loving It!"
      Case "2"
            Selection.Text = _
            "Not the most wonderful but still quite good."
      Case "3"
            Selection.Text = _
            "It's OK I guess. Seen better and seen worse."
      Case "4"
            Selection.Text = _
            "It's a start but it needs work."
      Case "5"
            Selection.Text = _
            "What a pile of GARBAGE!"
      Case Else
            Selection.Text = _
            "Must be a number from 1 to 5. Try again."
      End Select
End Sub


It's a pretty simple system. The user enters a number and clicks a Button. Then the Word VBA macro above replaces the number with a phrase. And it could use some improvement. For example, the program depends on the existance of a Bookmark named "Rating" in the document. This can make the program fail to work after a fairly simple change - deleting the part of the document containing the Bookmark. (This technique works best if the document is a read-only "Template" so the user doesn't change anything accidentally.)

The number that was entered is then Selected ...

Selection.MoveRight _
   Unit:=wdCharacter, Count:=1, Extend:=wdExtend


... and then overwritten ...

Selection.TypeText ""

... just to make the result look a little nicer. Again, this simple macro isn't very foolproof and in production systems, you're going to want to spend a lot more time thinking about what your users are actually going to do when then run the system.

Finally, the rating text is inserted into the document using a Select Case statement:

Select Case theRating
Case "1"
   Selection.Text = _
   "Huzza! Great! Loving It!"
etc. ...


As simple as it is, this system does illustrate a lot of important techniques that you can use to build a much more complicated system. If you're interested in trying a little more complicated system using both Word 2007 and Excel 2007 together, try this article!
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.