Editing and Viewing Macro VBA Code

3 4

If you encounter any undesirable traits or errors when you test a macro, you can edit the macro. To do so, you open the macro in the VBE, where you can make corrections, remove unnecessary steps, rename or copy the macro, or add instructions that you can't record in Word. To open a macro for editing in the VBE, follow these steps:

  1. Choose Tools, Macro, Macros. The Macros dialog box opens.
  2. In the Macro Name list, select the name of the macro you want to edit. If the macro you're looking for isn't displayed, click the Macros In down arrow, and select a different document or template in the drop-down list.
  3. Click Edit. The VBE appears, as shown in Figure 40-9.

Note


If you select a macro that's read-only, the Edit button will be disabled and appear dimmed.

Of course, if you prefer not to edit the VBA code to correct your macro, you can simply delete the macro and run the macro recorder again. Deleting macros is described in the section, "Deleting Macros and Macro Projects."

figure 40-9. the vbe shows you the vba code used to run your macros.

Figure 40-9. The VBE shows you the VBA code used to run your macros.


Keeping Your Macro Code Trim

When you record a macro and you add a command that displays a dialog box (such as choosing Format, Paragraph to open the Paragraph dialog box), Word records everything in the dialog box. In contrast, clicking toolbar buttons creates much more concise code. For example, let's say that part of your macro calls for right-aligning a paragraph. If you choose Format, Paragraph and set right-alignment by configuring the Paragraph dialog box settings while recording a macro, the following information will be inserted into your macro's code. (Notice that the alignment information is contained on a single line.)

 With Selection.ParagraphFormat         .LeftIndent = InchesToPoints(0)         .RightIndent = InchesToPoints(0)         .SpaceBefore = 0         .SpaceBeforeAuto = False         .SpaceAfter = 0         .SpaceAfterAuto = False         .LineSpacingRule = wdLineSpaceSingle         .Alignment = wdAlignParagraphRight         .WidowControl = True         .KeepWithNext = False         .KeepTogether = False         .PageBreakBefore = False         .NoLineNumber = False         .Hyphenation = True         .FirstLineIndent = InchesToPoints(0)         .OutlineLevel = wdOutlineLevelBodyText         .CharacterUnitLeftIndent = 0         .CharacterUnitRightIndent = 0         .CharacterUnitFirstLineIndent = 0         .LineUnitBefore = 0         .LineUnitAfter = 0     End With 

In contrast, if you click the Align Right button on the Formatting toolbar instead of opening the Paragraph dialog box, Word creates the following code for your macro:

 Selection.ParagraphFormat.Alignment = wdAlignParagraphRight 

In either case, your paragraph will be right-aligned, but the amount of VBA code used to create the effect differs dramatically. As you'd expect, using the single line of code approach is preferable because it's the more processor-efficient way of accomplishing a task.

You can compare how various procedures appear in VBA by creating your macro and noting the commands you use. Then view your macro's code in the VBE. You can rework your macro and view the VBA code each time until you find the combination that seems the most efficient.




Microsoft Word Version 2002 Inside Out
Microsoft Word Version 2002 Inside Out (Inside Out (Microsoft))
ISBN: 0735612781
EAN: 2147483647
Year: 2005
Pages: 337

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net