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

Learn VBA Macro Coding with Word 2007 - A "From the Ground Up" Tutorial
Part 4 of an About Visual Basic Tutorial

By Dan Mabbutt, About.com

May 30 2008

Adding to the System

This is a free tutorial to help non-programmers learn to write a computer program using the language built into Microsoft Word 2007: VBA. You can multiply your productivity in using Word by writing your own software to automate the things you do with Word. Do you have to write letters where only a few things change? Just type in the things that change and let a program write the letter.

To get the most from this tutorial, you might want to start at the beginning: Part 1 - A "From the Ground Up" Tutorial - Write a Program! Now!

The Parts Are Greater Than The Whole

A core idea of OOP is that you shouldn't have to write the whole program yourself. You should be able to take advantage of objects that have been written by other programmers! We followed that idea in one direction by using objects that are part of Word and Visual Basic to create the code we have so far. Now, we're going to consider a new idea: a category of objects that are called controls. The 'traditional' controls used in VBA are called ActiveX controls. Word 2007 has added some new tricks and one is called Content controls. There's a short intro in a few pages.

Controls are common to just about every Visual Basic environment you will see and form the basic building blocks of most applications. You use them by drawing them onto a UserForm object in your VBA application, or even right into the document itself. After you have completed a few VBA systems, you will know them like old friends!

To learn about the controls, let's look at two in more detail: the ComboBox and the CommandButton. Most controls work in a very similar way. To 'follow along' with this discussion, you might want to add these controls to your document. Just click Design Mode in the Controls box under the Developer tab of your document.

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

If you double click a control in Design Mode, Word opens the Visual Basic editor with an initial default event subroutine already coded for you in the code window. But if you click the same control in normal operation, the Click event subroutine for that control is executed. (If it exists.) For most purposes, adding a control directly into the document isn't what you want to do because when the document prints, the control will print as a graphic along with the document.

Most people find it very easy to add controls to a UserForm or to a document and then adjust their size and position by dragging the control and changing the size with the mouse. Figuring out how to use all the properties, methods and events for controls is where the real work is. For example, there are almost fifty properties for the ComboBox that can be set for different purposes.

The first property of a control that you will want to change is the name. Some properties can be set by programming statements (at "run time") or in advance (at "design time"). For example, if you wanted to highlight a ComboBox at run time to alert the user, one way to do it would be to add this code to a module that is executed.

ComboBox1.BackColor = wdColorRed

This statement uses a 'built-in' constant value, wdColorRed, to identify the color. You can find a complete list of these using the Object Browser in the Visual Basic editor (search for "wdColor"). Also notice that I used the default name ComboBox1 of the control rather than changing it to something more meaningful like cmbAccountNum since this is just an example. When you write your own code, change the names.

To try out this statement, add a ComboBox and a CommandButton to the document and double click the CommandButton in Design Mode to open the code window for the Click event.

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

To try it out, turn off Design mode again and click the CommandButton1 control.

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

A ComboBox is a good example of a type of control that you can use as a place to store lists of information that your program needs. Some examples might be:

  • The names and standard abbreviations of states in the US
  • The color codes and their values used with Visual Basic controls
  • The titles and show times of all the movies playing in your city

As you can see, there are a lot of different kinds of information that work well with this control and that is one of the reasons it's used so frequently.

With this introduction in mind, let's actually add some controls to our program.

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. Learn VBA Macro Coding with Word 2007 - A "From the Ground Up" Tutorial

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

All rights reserved.