Just as an application is rich in features and functionality, the equivalent object model is filled with members that represent the same features and functionality. An average object model in Office contains hundreds of members, including collections, objects, properties, methods, and events. To learn all of the members of a model, you may have to switch frequently between the corresponding Visual Basic Help file and the Visual Basic Editor window.
Visual Basic for Applications provides four tools that simplify the search for the list of properties, methods, or events that an object supports. They are the Macro Recorder, the Object Browser, Auto List Members, and Auto Quick Info.
Word, Excel, and PowerPoint provide a macro recorder that helps you quickly learn the object model of the application. (Access and Outlook don't provide a macro recorder.) Macro recording provides the equivalent Visual Basic code of an action that you conduct through an application's graphical interface.
For example, to change the color of a shape on a PowerPoint slide, you select the shape, click AutoShape on the Format menu, and then select a fill color from the Color drop-down list (in the Color And Lines tab). The equivalent macro-recorded code is as follows:
Sub Macro1()" Macro recorded 1/1/99 by David Boctor' ActiveWindow.Selection.SlideRange.Shapes _ .AddShape(msoShapeRectangle, 246, _ 288, 138, 132).Select With ActiveWindow.Selection.ShapeRange .Fill.Visible = msoTrue .Fill.Solid .Fill.ForeColor.RGB = RGB(128, 0, 0) End With End Sub |
NOTE
Macro recording is selection-based. This means that the Macro Recorder will record actions on selected objects in an active window.
The macro-recorded code gives you a good start on whatever you might want to do or create with Visual Basic code. Look at the first working line of code in the macro above and you'll see how to add a shape to a slide. If you want to add a shape to a specific slide (for example, the second one) rather than the slide in the active window, you can replace the code in the Macro1 procedure with the following code in Macro2:
Sub Macro2() Dim NewShape As Shape Set NewShape = ActivePresentation.Slides(2). _ Shapes.AddShape(msoShapeRectangle, 246, _ 288, 138, 132) End Sub |
Make sure you have at least two slides in the active presentation. The code above adds a rectangle to the second slide. This line closely resembles the first line of the recorded macro. You just replaced ActiveWindow.Selection.SlideRange with ActivePresentation.Slides(2) so that your code works on a specific slide; you don't have to have the slide displayed in the active window. This macro also demonstrates how to declare a variable (NewShape) that represents the new shape that's added to the slide. You'll learn more about declaring variables and working with shapes and other objects later in this book.
Although most macro-recorded code doesn't provide the exact code you need for your solution, it does provide the exact syntax you need to manipulate many objects within Word, Excel, and PowerPoint. It also helps you write Visual Basic code without constantly searching through the Help file.
A macro is a Visual Basic for Applications program that automates a series of actions. These can range from a simple procedure you conduct with your document content and application tools to complex solutions that manipulate your document content and interact with other Office features. A macro resides within the Visual Basic project of a Word document, an Excel workbook, a PowerPoint presentation, or an Access database. Because macros are usually actions grouped together, they enable you to accomplish a series of common, often repetitive, tasks automatically with a single command. You often create simple macros to make editing and formatting tasks readily available through a dialog box.
Word, Excel, and PowerPoint each offer an easy way to create a simple macro: with the Macro Recorder. Access and Outlook don't provide a macro recorder. You can see a recorded macro's contents in the Visual Basic Editor, where you can easily modify the macro. Recording a macro is like recording music or video. When you play it back, it automatically repeats recorded actions.
Each macro you record is stored in a Visual Basic code module that's attached to the open document, workbook, presentation, or template. Using the Visual Basic Editor, you can edit macros or move macros from one code module to another in any open Visual Basic project.
Imagine that you want to manually change the color of a shape on a PowerPoint slide. To do that you have to select the shape, choose AutoShape from the Format menu, and then select a color from the Fill Color drop-down list (in the Color And Lines tab in the Format AutoShape dialog box). To determine the equivalent functionality in Visual Basic syntax isn't necessarily a straightforward task for a beginner or even an experienced Visual Basic programmer. Fortunately, macro recording provides what you need without much effort.
The Record Macro dialog box is displayed. Notice the default macro name in the Macro Name box and the presentation name in which the recorded macro will be stored.
Macro recording has started, and the Stop Recording toolbar is displayed on your screen.
Until macro recording is stopped, the equivalent Visual Basic code of most of the actions you conduct is recorded in a code module in the Visual Basic Editor.
Rectangle
Stop Recording
This is the presentation that you selected to store the recorded macro.
The following code was recorded:
Sub Macro1() ' ' Macro recorded 1/1/99 by David Boctor ' ActiveWindow.Selection.SlideRange.Shapes _ .AddShape(msoShapeRectangle, 246, _ 288, 138, 132).Select With ActiveWindow.Selection.ShapeRange .Fill.Visible = msoTrue .Fill.Solid .Fill.ForeColor.RGB = RGB(128, 0, 0) End With End Sub |
Your recorded code may be slightly different. This macro is currently stored in the Visual Basic project of the active presentation.
View Microsoft PowerPoint
On the new slide, Visual Basic automatically re-creates the same shape in the same color that you previously created manually.
Many road maps have an index that lists all the locations on the map. To give the Visual Basic programmer a similar guide for navigating through an object model, the Visual Basic Editor provides a tool window called the Object Browser. By learning to use it effectively you'll save a lot of time.
The Object Browser's window contains five main elements: the Project/Library drop-down list, a Search text box with Search Results box, the Classes list, the Members Of list, and the Details pane.
You'll find the object model definition for each application in Office (and for any other application that supports an object model) in a file referred to as an object library. An object library's filename usually contains the extension .olb. However, sometimes an object library is contained within a dynamic-link library (.dll) file. Generally, the object library file is just an information file for whatever objects, properties, methods, events, and enumerations an application exposes. By providing an object library, Visual Basic can search for the objects an application exposes without having to load the entire application itself and then ask it for a list of exposed objects.
In Part 2 of this book, you'll see that referencing an object library is important when you program Office using objects and functionality from multiple applications. That's because you can do it all from within one Visual Basic Editor window.
For example, you can start PowerPoint, display its Visual Basic Editor, and press F2 for the Object Browser. In the Project/Library drop-down list, select PowerPoint. In the Classes list, you'll see only the objects that PowerPoint exposes. The list has a number of objects, including familiar ones such as Presentation, Slide, and Shape.
If you select PowerPoint from the Project/Library drop-down list and then select Presentation from the Classes list, in the Members Of Presentation list you'll see the methods and properties that you might know from the PowerPoint menus and toolbars. Familiar methods include Close, Save, SaveAs, and PrintOut, while common properties include Name, Path, and FullName.
If you select Presentation from the Classes list and then PageSetup in the Members Of list, the Details pane displays the following information:
Note that the last word in the first line in the Details pane, "PageSetup," is underlined and green. This indicates two things: (1) the PageSetup property returns an object called PageSetup, and (2) you can navigate to the PageSetup object by clicking the link.
The Search Results box gives you a quick way to determine which members in an object library support certain properties, methods, or events. After searching through the specified object library or libraries, it lists members that match the criteria you specified in the Search text box. If you select <All Libraries> in the Project/Library box, the search is conducted in all the libraries in the Project/Library list; if you select a specific library, the search scans that library only.
Search button
The first column in the Search Results list is the Library; the second column is the Class, or object; and the third is the Member property. Selecting any row in the Search Results list refreshes the contents of the Classes list and the Members Of list, so you can navigate to the exact location of the search result item in the object library.
One of the Visual Basic Editor's newest and most exciting additions is the Auto List Members drop-down list, which you saw briefly in a previous example. Because of it, you never have to memorize the methods or properties of an object again. All you need to do is start typing, and once you type a period (.) after a valid object name, Auto List Members automatically displays a drop-down list of all the properties and methods supported by the object. You can scroll down the list with the mouse, or you can continue typing the method or property name. If you continue typing, Auto List Members selects an item in the list that matches your typing.
Imagine that you want to define a name of an object within one of your procedures.
MsgBox sldSlide.Name |
When you start to use a term or variable that the Editor doesn't recognize, you have to do all of the work yourself. In this case, you must already know that sldSlide is the name of a slide and that a Name property applies to slides.
When you declare an object variable, you also take advantage of the Auto List Members tool. The Auto List Members drop-down list displays information that would logically complete the statement at the current insertion point.
Dim sldSlide As Slide Set sldSlide = ActivePresentation.Slides(1) |
You've now declared the sldSlide variable as representing a PowerPoint Slide object, and the Visual Basic Editor now knows what type of object this variable references. Your procedure should look like this:
Sub WithoutDeclaration() Dim sldSlide As Slide Set sldSlide = ActivePresentation.Slides(1) MsgBox sldSlide.Name End Sub |
MsgBox sldSlide.Name |
When you start typing the line above and get to the point where you type a period (.) after sldSlide, the Auto List Members drop-down list appears.
At this point, you can scroll down the list and select the property or method item, or you can continue to type the name of the property or method if you know it. As you continue to type, the list automatically scrolls down to find a match for the text you started to type. If the property or method name is long and the item is selected in the Auto List Members drop-down list, you can press TAB to insert the item in your line of code.
As you write your code, you have a number of ways to determine the exact syntax of the object, methods, and properties you use. One of the easiest is to display the Auto Quick Info window, which shows function information and parameters as you type. The Auto Quick Info window is similar to the ToolTip you see when the cursor is over a toolbar button.
In the "Pass Arguments to the SaveAs Method in PowerPoint" section of this chapter, when you pressed the spacebar after typing the SaveAs method name, the Auto Quick Info window appeared as follows:
You can see in the Auto Quick Info window that the SaveAs method of the Presentation object in PowerPoint takes three arguments. Each argument in the Auto Quick Info window is separated by a comma (,); as you type a comma, the next argument in the window becomes bold. Some arguments are encased in square brackets, which indicate that specifying the argument is optional. In the SaveAs method above, the first argument is required and the second and third are optional. If the argument is optional, the application defines a default value for it.
In the SaveAs method, the second argument, FileFormat, defaults to the current version of a PowerPoint presentation. You can also see in the case of the second argument that when you type a comma, both the Auto Quick Info window and the Auto List Members drop-down list are displayed. The Auto List Members drop-down list is displayed because the FileFormat argument is one of the enumeration values (PpSaveAsFileType) in the list.
TIP
When both the Auto Quick Info window and the Auto List Members drop-down list are displayed, you can toggle between them by clicking either one to put it in front of the other.