Recording a Macro to Generate Code

Microsoft Word Visual Basic

Recording a Macro to Generate Code

   

If you are unsure of which Visual Basic method or property to use, you can turn on the macro recorder and manually perform the action. The macro recorder translates your actions into Visual Basic code. After you've recorded your actions, you can modify the code to do exactly what you want. For example if you don't know what property or method to use to indent a paragraph, do the following:

  1. On the Tools menu, point to Macro, and then click Record New Macro.
  2. Change the default macro name if you'd like and click OK to start the recorder.
  3. On the Format menu, choose Paragraph.
  4. Change the left paragraph indent value and click OK.
  5. Click the Stop Recording button on the Stop Recording toolbar.
  6. On the Tools menu, point to Macro, and then click Macros.
  7. Select the macro name from Step 2 and click the Edit button.

View the Visual Basic code to determine the property that corresponds to the left paragraph indent (the LeftIndent property). Position the insertion point within LeftIndent and press F1 or click the Help button. Within the topic, you can view examples and review the objects that support the LeftIndent property (click Applies To).

Remarks

Recorded macros use the Selection property to return the Selection object. For example, the following instruction indents the selected paragraphs by a half inch.

Sub IndentParagraph()
    Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.5)
End Sub

You can, however, modify the recorded macro to work with Range objects. For information, see Revising recorded Visual Basic macros.