
01 0789725150 Web 1 5/22/01 8:14 AM Page 1 BONUS WORD CHAPTER Getting Started with VBA In this chapter by Bill Ray What Is Visual Basic for Applications? 2 Reading and Editing Your Recorded VBA Code 4 Using the VBE to Create Accurate Code 15 Understanding Projects and Modules 19 Saving Changes to Your Macros 20 Understanding the VBA Help System 21 Troubleshooting 22 Using Copyright © 2001 01 0789725150 Web 1 5/22/01 8:14 AM Page 2 Bonus Word Chapter Getting Started with VBA 2 What Is Visual Basic for Applications? Visual Basic for Applications (VBA) is the main programming tool for automating the Microsoft Office family of applications. VBA is derived from one of the most widely used programming languages for personal computers: BASIC (short for Beginner’s All-purpose Symbolic Instruction Code). Earlier versions of many Microsoft applications used variations of BASIC as their macro language, but until recently, many of these languages had signifi- cant differences from one another. Microsoft had been working for years to alleviate this problem and unify the macro language offerings available for Office applications. Because Visual Basic is one of the most popular Windows application development lan- guages in the world and it allows developers to create standalone, executable Windows applications, many VB programmers wanted Microsoft to create an automation language for its Office suite that was based on the language. It certainly didn’t hurt that it was also a tool that they were already familiar with, and Microsoft listened. The result is Visual Basic for Applications (VBA), and it is the automation language used with Word, Excel, Outlook, Access, and PowerPoint. Besides the Office applications, other Microsoft programs, such as Project, use VBA. Microsoft has even licensed VBA for use in third-party applications. You can think of VBA as a subset of Visual Basic. Although VBA is a subset, it still offers awe- some programming power and a feature-rich development environment. VBA is a powerful programming environment, providing a full-featured programming lan- guage development environment. It is the latest in object-based programming, yet it remains easy to use. Microsoft has taken great effort to build online and context-sensitive help into the tools associated with VBA. VBA’s biggest advantage, perhaps, is its consistency from one application to another. After you learn to program in VBA in Word, you have a pretty good head start to programming in, for example, Excel or PowerPoint. In this scenario, all you have to do is learn the specific features for working with a spreadsheet or with presentation graphics. Is there any difference between a macro and a program? Not really. Macro has been the common name for the automation tools built into many programs ever when Lotus 1-2-3 and WordPerfect dominated the PC marketplace. By most definitions, any sequence of commands that give instructions to be carried out by a computer is a pro- gram. Give yourself credit: If you write macros, you’re a programmer! How VBA Relates to WordBasic For all versions of Word for Windows through Word 95 (also known as Word 7.0), Word’s macro language was WordBasic. VBA was introduced as Word’s macro language beginning with Word 97. Because WordBasic is also a dialect of the BASIC programming language, it has some similarity to VBA. However, there are substantial differences between the two languages, especially in the way that Word commands are performed in the two languages. Special Edition Using Microsoft Word 2002 © Que 2001 01 0789725150 Web 1 5/22/01 8:14 AM Page 3 What Is Visual Basic for Applications? 3 Although versions of Word since Word 97 (2000 and 2002) no longer directly support WordBasic, you don’t necessarily have to re-create your old macros from scratch when upgrading from an old version. When you open a Word 6 or Word 95 template in Word 2002, Word automatically translates the existing WordBasic macros into a format that VBA can use. You can even edit these converted macros if you need to make changes in their functionality. In the long run, you’ll probably want to convert your existing WordBasic macros to VBA to take fullest advantage of the newer features of Word 2002. When you open an existing Word 6 or Word 95 template in Word 2002, Word saves the template in the new file format, which can’t be read in the earlier Word versions. If you will be continuing to work in Word 6 or Word 95, make sure that you only work with copies of the original templates. Keep in mind that the automatic conversion of WordBasic macro code into VBA doesn’t always work perfectly, especially if your WordBasic macros are complex. Test your con- verted macros carefully to verify that they work the same way they did in WordBasic. If they don’t, you might have to edit the macros, or re-create them in VBA. When to Use VBA Although recorded macros can do just about anything in Word, it’s often useful to be able to edit macros or create new macros by writing your own VBA code. The following situa- tions are just a few examples: I You have recorded a macro that works well, but now you want to change the way it works without recording it again. I You want to prompt the user to input information, such as his name or a filename. I You want to display one of Word’s dialog boxes so that a user can make selections from the dialog box. I You want your macro to make logical decisions. For example, you can program a macro to function only if your current selection is in a table, or if the document is in Page Layout view. I You want to repeat an action many times. For example, a macro can apply header or footer formatting to each section in a long document. I You want to exchange data with other Office applications, or even perform actions using those applications. I A long, complex macro runs slowly, and you want to improve its performance. Special Edition Using Microsoft Word 2002 © Que 2001 01 0789725150 Web 1 5/22/01 8:14 AM Page 4 Bonus Word Chapter Getting Started with VBA 4 I You made a mistake while recording a macro, but you don’t want to throw away the whole macro. I The VBA macro recorder doesn’t always produce the most efficient VBA code. You can often improve the performance of your macros by editing the recorded code. VBA is a hosted language. To use it, you must be running a Visual Basic Editor (explained n the following section) from within a hosting application, such as Word or another Office program. Reading and Editing Your Recorded VBA Code The best way to get started with the Visual Basic Editor is to record a macro and then to study the code you’ve recorded. For example, you can record a macro that adds a place for your signature to the end of any document. Follow these steps to record the macro: 1. Open a document to use as a test document while you are recording the macro. 2. Select Tools, Macro, Record New Macro (or double-click the REC button on the status bar) to open the Record Macro dialog box. 3. Type Signature as the macro name and click OK. 4. Press Ctrl+End to move to the end of the document. 5. Press Enter twice to add a paragraph and a blank line. 6. Type Sincerely and then press Enter four times. 7. Type your name and press Enter. 8. Press Ctrl+I to turn on italic. 9. Type your title; then press Ctrl+I to turn off italic. 10. Press Enter to add another paragraph. 11. Click the Stop button or double-click the REC button on the status bar to turn off the macro recorder. ➔ To find more information about recording macros with Word 2002, see Chapter 28, “Recording and Running Visual Basic Macros,” p. 871. Now that you have recorded a macro, you can open the Visual Basic Editor to view the VBA code you just created. 1. Choose Tools, Macro, Macros to show the Macros dialog box. 2. Select the name of the macro you just recorded from the Macro Name list box. 3. Choose Edit to open the Visual Basic Editor, displaying the selected macro, as shown in Figure 1. Special Edition Using Microsoft Word 2002 © Que 2001 01 0789725150 Web 1 5/22/01 8:14 AM Page 5 Reading and Editing Your Recorded VBA Code 5 Project Explorer Standard Toolbar Figure 1 You can use the Visual Basic Editor to create, edit, debug, and manage your VBA macros. Properties Window Code Window Tip from If you don’t need to work on a particular existing macro, you can quickly activate the Bill Ray Visual Basic Editor by pressing Alt+F11 or by choosing Tools, Macros, Visual Basic Editor. The Visual Basic Editor (VBE) contains the common set of macro programming tools for editing VBA code in Office XP. All of Office XP’s applications use the VBE to create and edit VBA code. The VBE provides a wide variety of features to assist you in creating VBA macros: I A code editing window for viewing and editing VBA code I Form design tools for creating custom dialog boxes I Tools that enable you to organize your code and work with several projects and modules at once I Debugging tools to help you create and test complex macros Throughout the remainder of this chapter, you’ll learn about each of the tools available in the VBE.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages310 Page
-
File Size-