1. Computing

Learn VBA Macro Coding with Word 2007 - A "From the Ground Up" Tutorial

Part 3 of an About Visual Basic Tutorial

From , former About.com Guide

See More About
Updated March 11, 2010

Your First 'Job Worthy' Program

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!

Analysis - Planning Your Work

The first step in any programming project should be a careful plan of exactly what the program is intended to do. Most programmers call this analysis and today, it has become a separate job skill, 'Analyst', with software and methods focused exclusively on this one job. Especially as a new programmer, you don't have to complete a formal analysis document. But you should take a few moments to make sure you know what you're trying to do before starting to code.

In this segment of the tutorial, we write a VBA program that you might actually want to modify for your own use. The "Form Letter" macro will substitute some text into a template document and save it.

As noted earlier, Microsoft is trying very hard to move away from VBA and Word 2007 has some new tricks (Content Controls) that do this same job. I'll introduce them in the next part of this tutorial, but our purpose here is to learn to program VBA.

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

One of the purposes of this excercise is to start to train your mind to 'see' the things that need more careful consideration. For example, since the diagram shows that you will have a file in and a file out, perhaps there should be some way of providing the names of those files to the program. Maybe there ought to be a way to tell program where the files are located. And maybe there will be more than one file created at some point?

The same is true for our "Name" and "Great Work Description" data items. How will this information be provided to the program?

Another "best practice" in analysis is that you don't have to solve the whole problem with the first program. Sometimes it's best to write a simple program that just gets you part of the way and plan on a more complete version later on. In fact, this is exactly what you do when you use the macro recorder to create an initial program. Our first version of the Form Letter program won't have any way to change the values filled into the form letter.

Although a more complex program would justify a great deal more analysis, this is enough for now. So ... lets crank up Word 2007 and the macro recorder again to write another program on the next page.

  1. About.com
  2. Computing
  3. Visual Basic

©2013 About.com. All rights reserved.