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

Learn VBA Macro Coding with Word 2007 - A "From the Ground Up" Tutorial
Put Your Assistant To Work Again

By Dan Mabbutt, About.com

May 24 2008

Start Word 2007 again. Step-by-step instructions for creating the macro are below. If you need more help remembering exactly how to do all this, review Part 1 again.

This time we will name the VBA macro AboutFormLetter. The document will be named AboutVB3.docm. If you're new to programming, you might be struggling with how to organize your files on your computer as well. It's a good idea to create a new folder for a project like this to avoid mixing files together and forgetting which is which. Create a new folder somewhere on your computer (you could call it FormLetter or About Visual Basic Project).

Here's some suggested text for your new form letter. We'll use this text later as we complete the macro.

The Form Letter

Dear (1),

I'm planning on writing (2) and (Lucky YOU!)
I'm going to give your publishing company the
chance to publish it first! Simply send the
cost of mailing twenty pounds of paper
to the return address on this letter, and
I'll get started writing it right away.

The text (1) and (2) are called placeholders. The VBA macro we will write will replace the placeholders with other text. There are alternative, more sophisticated ways to do this, but this will do the job for now.

Recording the AboutFormLetter macro

To construct the initial macro with the macro recorder, follow the steps below. It's important that you do the steps exactly, because any change will be reflected in the macro that is being recorded. You might have to start over and try it again to get it exactly right. If you do, just click "Yes" when Word asks if you want to replace the existing macro. If you start over, remember to start with the original document that has the (1) and (2) placeholders again.

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

  1. Open a new document.
  2. Save it using the file name AboutVB3.docm. Make sure that it's saved as a "Word Macro-Enabled Document."
  3. Type the suggested text above into the letter, or type your own letter! Be sure to include the placeholders - (1) and (2) - in the document.
  4. Save and close the document. We go through this extra step so we can include the "Open" operation into the macro when we turn on the recorder in the next step.
  5. Turn on the macro recorder. Use the name AboutFormLetter for the macro. Because we closed the document, we can't store it in this document right now. We have to select the Normal template as a place to store the it and plan on moving it later.
  6. Open the AboutVB3.docm document again.
  7. Find the text (1) in the document and replace it with "Mr. Publisher Dude".
  8. Find the text (2) in the document and replace it with the text "the Great Novel of Our Time".
  9. Close the Find dialog and save the document using the file name AboutVB3_01.docm.
  10. Turn off the macro recorder.

Now that we have created our initial VBA macro, let's see what we have on the next page.

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.