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:


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


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


Click the Assign Macro To Keyboard button.


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!)


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


Click the Center tool on the Formatting toolbar.


Click the Stop Recording tool on the Stop Recording toolbar.


Open some other document.


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


Press Ctrl+F8.


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:


Choose Macro|/Macros on the Tools menu.


Select the macro that you just created.


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



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


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


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.