Editing and Viewing the VBA Code

 < Day Day Up > 



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 arrow, and select a different document or template in the list.

  3. Click Edit. The VBE appears, as shown in Figure 32-9.

    click to expand
    Figure 32-9: The VBE shows you the VBA code used to run your macros. This macro shows code used to insert a logo graphic.

Note 

If you select a macro that's read-only, the Edit button will be disabled and appear dimmed. Similarly, you can't edit macros in read-only documents.

start sidebar
Keeping Your Macro Code Trim

When you record a macro and 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 repeatedly creating your macro, noting the different commands you use, and viewing 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's the most efficient.

end sidebar

Tip 

Avoid editing VBA code  If you prefer not to edit the VBA code to correct a macro, you can correct problems in many cases by deleting the macro and running the macro recorder again. Deleting macros is described in "Deleting Macros and Macro Projects".



 < Day Day Up > 



Microsoft Office Word 2003 Inside Out
Microsoft Office Word 2003 Inside Out (Bpg-Inside Out)
ISBN: 0735615152
EAN: 2147483647
Year: 2005
Pages: 373

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