Special Edition Using Microsoft Office FrontPage 2003
Visual Basic for Applications (VBA) is easily one of the most powerful features of FrontPage. It is also one of the least utilized. Unlike most other Office applications, FrontPage does not provide a macro recorder. If you want a macro for FrontPage, you have to write it yourself, and many users of FrontPage are uneasy about delving into this area. In fact, writing macros is much easier than most people think, and once you start writing code, you'll find that there's almost no limit to what you can accomplish. The purpose of this chapter is not to teach you the syntax of VBA code or to teach you all the facets of programming macros in FrontPage. Instead, the intent is to give you enough information to get your feet wet. It's important to realize that the best method for learning how to write any kind of code is to jump right in and start coding. Hopefully, this chapter will provide you with the foundation to do just that. Macros are developed with the Visual Basic Editor. To start the Visual Basic Editor, select Tools, Macro, Visual Basic Editor, or use the keyboard shortcut Alt+F11. NOTE Even though Microsoft calls the macro editor the Visual Basic Editor, it is not the same as Microsoft Visual Basic. When you are writing macro code in FrontPage or any other Office product, you are writing VBA code. VBA is a subset of Visual Basic and does not have all the same capabilities. One of the limitations of VBA is that the code must run within a host application. You cannot write a standalone application with VBA as you can with Visual Basic.
When you first launch the Visual Basic Editor, you will see two windows on the left side of the screen as shown in Figure 30.1 the Project Explorer and the Properties window. The Project Explorer displays a tree view of all files that are part of your macro project. The Properties window provides access to the properties of items within your project. Let's briefly discuss the files that make up a macro project. Figure 30.1. The Visual Basic Editor provides all the tools necessary to write macros for FrontPage.
Understanding Modules, Class Modules, and Userforms
Three types of files are available to you in a macro project; modules, class modules, and UserForms. Modules and class modules contain only code, whereas UserForms contain both code and user-interface elements. A macro project must have at least one module. It can also contain one or more class modules and UserForms, but is not required to have either. Module
A module is a file that contains VBA code. It is common practice for VBA programmers to create many modules when programming complex macros. For example, you might have one module that contains code to perform certain editing functions in your Web pages and another module that performs specialized tasks such as sending email if a failure occurs. The choice is yours, but organization is critical if you want to keep track of your code. When you start the Visual Basic Editor, one module is automatically created for you. To create new modules, right-click inside the Project Explorer and select Insert, Module from the menu. NOTE The time to get organized is before you write your first line of code. When approaching a macro project, try to break your project into logical sections, and then clearly define the requirements for each section.
Class Module
A class module is a special kind of module that defines a class. A class is an object that has definable characteristics and performs certain tasks. Characteristics of a class are called properties, and the code that enables the class to perform a certain task is called a method. FrontPage has many of its own classes that you can use, but you can also create your own classes with their own properties and methods. Any classes you create are defined in class modules. To insert a new class module into a macro project, right-click in the Project Explorer and select Insert, Class Module. The use of class modules is outside the scope of this book. NOTE For more information on defining classes within a class module you can check out Special Edition Using Visual Basic 6.0. Although it discusses Visual Basic and not VBA, the concepts of classes and class modules does not differ between VBA and Visual Basic.
UserForm
A UserForm provides a user interface for your macro. Many macros do not include a UserForm, but if you require more than basic interaction with the users of your macro, a UserForm might be required. To add a new UserForm to your macro project, right-click in the Project Explorer and select Insert, UserForm as seen in Figure 30.2. We will be designing a UserForm for the sample macro we'll write later in this chapter. Figure 30.2. A UserForm allows users to interact with your macro.
|