Creating a macro with no programming experience using the recorder

Article contributed by Bill Coan

Word's macro recorder can help you acquaint yourself with macros and with Office 97's vba programming language.

Let's assume that you create several documents from scratch on a daily basis. After composing the text, you press Ctrl+Home to position the cursor at the start of the document. Then you click the Center tool on the Formatting toolbar to center the first paragraph of your document. Eventually, it occurs to you that you should be able to record a macro for these last actions. That is, a macro that will automatically position the cursor at the start of the document and center the first paragraph.

Technically, this scenario may call for a solution involving paragraph styles. But for now let's create a macro solution and let's use the macro recorder to create it.

Proceed as follows:

1.

Choose Macro|Record New Macro on the Tools menu (or simply double-click
the REC button in the Status bar.)

2.

Enter a macro name (sorry, no spaces or punctuation allowed).

3.

Click the Assign Macro To Keyboard button.

4.

Press Ctrl+F8, then click Assign and click Close. Word will display the Stop Recording toolbar, which lets you know that Word is recording your actions and will continue recording them until you click the Stop Recording button on this toolbar. (Don't click it yet!)

5.

Press Ctrl+Home to position your cursor at the start of your document.

6.

Click the Center tool on the Formatting toolbar.

7.

Click the Stop Recording tool on the Stop Recording toolbar.

8.

Open some other document.

9.

Position the cursor in the middle of the document (doesn't matter where).

10.

Press Ctrl+F8.

11.

Smile with satisfaction as Word moves the cursor to the start of the
document and centers the first paragraph.

So far, so good. Now to see what the macro looks like under the hood:
  

1.

Choose Macro|/Macros on the Tools menu.

2.

Select the macro that you just created.

3.

Click Edit.

Word will open up the vba editor and display something like the following macro:

Sub MyFirstRecordedMacro()
'
' MyFirstRecordedMacro Macro
' Macro recorded 10/31/98 by Elgin
'
Selection.HomeKey Unit:=wdStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

End Sub

  

4.

To learn more about the code, position the cursor somewhere in Selection.HomeKey and press F1 to view a related help topic.

5..

Close the help window and repeat Step 4 with the cursor in other locations.

6.

On the vba editor File menu, choose Close and Return to Microsoft Word.

If you have the patience to try these steps, you'll be well on your way. The rest is just details. There are lots of them but you can always search the vba help system and return to the newsgroups for additional information.