Working with the Visual Basic Editor


Microsoft Word comes with the Microsoft Visual Basic Editor (Figure 11). This program enables you to write macros from scratch and edit macros that were either manually written or written using the macro recorder. The Visual Basic Editor is a true programming environment that includes a variety of windows, commands, and buttons designed for programmers.

Figure 11. The Microsoft Visual Basic Editor programming environment for Word.


To take full advantage of the Visual Basic Editor, you need to know how to program in the VBA languge. But even without a full understanding of VBA, you may be able to edit macros you recorded to remove or modify macro steps. For example, suppose you made and fixed a mistake while recording a macro. You can edit the macro code to remove the step where you made the mistake and the step where you fixed it.

In this part of the chapter, I explain how you can use the Microsoft Visual Basic Editor to edit a macro. I also tell you how you can get started writing macros from scratch and where you can find more information about programming in VBA.

Tip

  • One way to learn more about VBA is to use the Visual Basic Editor to examine macros written by others, including Microsoft Corporation.


To edit a macro

1.

Choose Tools > Macro > Macros to display the Macros dialog (Figure 9).

2.

Select the macro you want to edit.

3.

Click Edit. Microsoft Visual Basic Editor launches and displays its windows (Figure 11).

4.

Edit the macro code in the Code window.

5.

Choose File > Save Template Name, or press .

6.

Choose Word > Close and Return to Microsoft Word, or press .

Tip

  • If you don't know a thing about VBA, in step 4, limit yourself to modifying commands that you can recognize. For example, in the macro shown in Figure 11, I could change the AutoFormatting options by changing some true settings to False or remove AutoFormatting completely by deleting the step that begins with Selection.Tables(1) AutoFormat. This is pretty easy to figure out, even without VBA programming experience.


To write a macro from scratch

1.

Choose Tools > Macro > Macros to display the Macros dialog (Figure 9).

2.

Enter a name for the macro in the Macro name box.

3.

Choose a location to store the macro from the Macros in pop-up menu (Figure 12).

Figure 12. The Macros in pop-up menu.


4.

If desired, enter a description for the macro by editing the contents of the Description box.

5.

Click Create. Microsoft Visual Basic Editor launches and displays its windows. The Code window includes a new Sub procedure named for the macro (Figure 13).

Figure 13. A new Sub procedure is created for the macro in the Code window.


6.

Enter the macro code in the Code window for the new Sub procedure.

7.

If desired, use commands under the Debug menu (Figure 14) to compile or step through your macro statements.

Figure 14. The Debug menu in the Visual Basic Editor. These commands help programmers find errors in VBA code.


8.

Choose File > Save Template Name, or press .

9.

Choose File > Close and Return to Microsoft Word, or press .

Tips

  • As you can see in Figure 13, the clean slate approach to writing macros requires an intimate knowledge of VBA.

  • Using Debug menu commands (Figure 14) can help identify syntax errors (Figure 15) that will prevent the macro from running properly. These commands can't, however, fix errors. That's up to you. (And please don't ask me for helpI just told you everything I know about VBA!)

    Figure 15. If you use the Compile command on the Debug menu to check VBA code that contains an error, a dialog like this appears. When you click OK, the offending code is highlighted in the Code window.





MIcrosoft Word 2004 for Mac OSX. Visual QuickStart Guide
MIcrosoft Word 2004 for Mac OSX. Visual QuickStart Guide
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 199

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