1 / 68

The Object Model

2. The Object Model. Visual Basic for Applications. 2. Objectives. In this tutorial, you will learn how to: Refer to the objects contained in an application’s object model Use the Object Browser in the Visual Basic Editor

zelda
Download Presentation

The Object Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2 The Object Model Visual Basic for Applications

  2. 2 Objectives • In this tutorial, you will learn how to: • Refer to the objects contained in an application’s object model • Use the Object Browser in the Visual Basic Editor • Enter instructions in the Immediate window in the Visual Basic Editor • Refer to the most commonly used objects in Excel, Word, and Access

  3. 2 Concept Lesson:Understanding the Object Model • Although all VBA-enabled applications share the same language, each contains its own unique set of objects, referred to as the application’s object model • A collection object, typically referred to simply as a collection, is a group of one or more individual objects treated as one unit

  4. 2 Simplified Object Modelfor a Library

  5. 2 Referring to the Objects Containedin an Application’s Object Model • You can refer to an individual object within a collection using either its name or its position number in the collection • The Application rectangle represents the Application object • Immediately below the Application rectangle is the Presentations (Presentation) rectangle, which represents both the Presentations collection and the individual Presentation objects within the collection

  6. 2 The Most Commonly Used PowerPoint Objects

  7. 2 Referring to the Objects Containedin an Application’s Object Model • The Slides (Slide) rectangle is one level down from the Presentations (Presentation) rectanglein the object model • The plural word Slides denotes the name of the collection, and the singular word Slide indicates the type of object included inthe collection

  8. 2 Referring to Objectsin VBA Code • You can use VBA to refer to any element in an application’s object model, but to do so you must specify the object’s exact location in the object model’s hierarchy • You begin at the top of the hierarchy, with the Application object, and then work your way down each level until you reach the desired element • You can refer to a specific object within a collection using either the object’s name, enclosed in quotation marks, or its position number within the collection, referred to as its index • In most collections, the index of the first object in the collection is 1

  9. 2 Referring to an Object’s Properties and Methods in VBA Code • A property is a characteristic of an object, and a method is an action that an object can perform on its own • The Application object has a Name property that stores the Application object’s name, and it has a Quit method that is used to end the application • You use the expression.property syntax in a VBA instruction to access an object’s property, and you use the expression.method syntax to invoke one of its methods

  10. 2 Referring to an Object’s Properties and Methods in VBA Code • Before you can customize an application, you need to become familiar with the objects in its object model • One way to do so is to use the Object Browser, which is available in the Visual Basic Editor

  11. 2 The Object Browser • The Object Browser allows you to browse through all available objects in an application and see their properties, methods, and events

  12. 2 The Object Browser • A class is the formal definition of an object; it defines the properties that control the object’s appearance, and the methods and events that control the object’s behavior • All of the information pertaining to an application’s object model is stored in an object library, which is simply a file

  13. 2 The PowerPoint Application Object’s Help Screen

  14. 2 Getting Help in theObject Browser • You can view an object’s Help screen by clicking the object’s class in the Classes list box, and then clicking the Help button located at the top of the Object Browser window • According to the Help screen, the Application object represents the entire Microsoft PowerPoint application • To view a listing of these lower-level objects, you need simply to click the Multiple Objects rectangle; doing so will open the Topics Found dialog box

  15. 2 Getting Help in theObject Browser • To view a Help screen for one of these objects, you select the object’s name in the list, and then click the Display button • You can view the Help screen for an individual Presentation object by clicking Presentation in the Presentations Collection Object Help screen

  16. 2 Presentations Collection Object Help Screen

  17. 2 Using the Immediate Window • In addition to the Code window, the Visual Basic Editor also provides an Immediate window into which you can enter VBA instructions

  18. 2 Using the Immediate Window • The Application.Quit instruction does not begin with a question mark • Because Quit is a method, which represents an action that an object can perform, it does not store a value that can be displayed, as does a property • The Quit method simply instructs the application toclose itself

  19. 2 Using the Immediate Window • In the ?Application.Presentations.Count instruction, Application.Presentations specifies the location of the Presentations collection in the object model, and Count specifies the desired property • The Application.Presentations.Count reference indicates that the Count property is a member of the Presentations collection, which is contained in the Application object • Applications.Presentations(2) is the expression, and it indicates that the object is the second Presentation object contained within the Presentations collection, which is contained within the Application object; Name is the property whose value you want to display

  20. 2 Using the Immediate Window

  21. 2 Immediate Window Showing Additional VBA Instructions and Their Results

  22. 2 Repeating and Deleting Instructionsin the Immediate Window • If you want to repeat an instruction, you can either retype the instruction after the last instruction in the Immediate window, or you can select the instruction in the Immediate window, copy it to the clipboard, and then paste it after the last instruction in the Immediate window

  23. 2 Printing Code from the Immediate Window • You can use either a text editor or a word processor—for example, WordPad or Word—to print the instructions entered in the Immediate window • Like the Object Browser and the Help screens, the Immediate window is an invaluable tool for learning an application’s object model

  24. 2 Summary To refer to an object in an application’sobject model: • Specify the object’s exact location in theobject model • If the object is a member of a collection, include either the object’s name or its index in a set of parentheses following the collection’s name To access the property of an object: • Use the expression.property syntax To invoke one of an object’s methods: • Use the expression.method syntax

  25. 2 Summary To view information about the various objects available to your application: • Open the Object Browser window by clicking the Object Browser button on the Visual Basic Editor’s Standard toolbar • Select the appropriate object library from the Project/Library list box • Click the appropriate keyword in the Classes list box, and then click the Help button

  26. 2 Summary To use the Immediate window in the Visual Basic Editor: • Open the Immediate window in the Visual Basic Editor by clicking View on the Visual Basic Editor menu bar, and then clicking Immediate window • Enter the instruction in the Immediate window • You can copy and paste instructions from one part of the Immediate window to another • Remove one or more lines from the Immediate window by selecting the lines you want to remove and then pressing either the Delete key or the Backspace key

  27. 2 Excel Lesson:Understanding the Excel Object Model • Figure 2-17 shows the portion of the Microsoft Excel object model that includes the most commonly used Excel objects

  28. 2 Understanding the Excel Object Model • A Range object in Excel is defined asa cell, a row,a column, ora selectionof cells containing one or more contiguous or noncontiguous blocks of cells

  29. 2 Using the ObjectBrowser in Excel • The Object Browser is a useful tool for understanding the structure of an object model and for studying the various objects included in the model, as well as the properties and methods of the objects • To access the Object Browser in Excel, use the steps on pages 102 to 105 of the textbook • A Multiple Objects rectangle representsthe next level of objects in the objectmodel hierarchy

  30. 2 Object Browser Window Showing the Excel Object Library

  31. 2 Microsoft Visual Basic Help Window

  32. 2 Using the ObjectBrowser in Excel

  33. 2 Using the ObjectBrowser in Excel • To view the next level of objects in the object model hierarchy use the steps on pages 106 to 108 of the textbook

  34. 2 Workbook Object Help Screen

  35. 2 Using the ImmediateWindow in Excel • Unlike the Code window, the Immediate window allows you to test a line of code without having to create and run anentire procedure • To open Martin’s workbook, save the workbook, and enter several VBA instructions into the Immediate window, use the steps on pages 108 to 112 ofthe textbook

  36. 2 QtrSales.xls Workbook

  37. 2 Visual Basic Editor Showing Open Project Explorer and Immediate Windows

  38. 2 VBA Instructions Entered in the Immediate Window

  39. 2 Referring to Range Objects • A Range object in Excel is defined as a cell, a row, a column, or a group of contiguous or noncontiguous cells • For referring to Range Objects, use the steps on pages 113 to 115 of the textbook

  40. 2 Additional Instruction Entered in the Immediate Window

  41. 2 Jackson Worksheet Showing Selection of a Range Object Consisting of Noncontiguous Cells

  42. 2 Word Lesson:Understanding the Word Object Model • Figure 2-31 shows the portion of the Microsoft Word object model that includes the most commonly used Word objects

  43. 2 Word Objects and Collections

  44. 2 Understanding the Word Object Model • A Range object in Word is defined as a contiguous area in a document; it can be assmall as the insertion point or as large as the entire document • Although a paragraph is a contiguous area in a document, it is considered a Paragraph object rather than a Range object • The Application object, which refers to Microsoft Word, contains one Document object in its Documents collection • The Document object’s name is Word Lesson.doc

  45. 2 Understanding the Word Object Model • The Word Lesson document also contains five sentences in its Sentences collection; each sentence is a Range object • Like the Sentences collection, the Words collection also consists of Range objects • The Characters collection also consistsof Range objects, but each Range object in this collection is composed of exactly one character

  46. 2 Using the Object Browser in Word • To access the Object Browser in Word use the steps on pages 122 to 125of the textbook

  47. 2 Microsoft Visual Basic Help Window

  48. 2 Quit Method Help Screen

  49. 2 Example Showing the QuitMethod in a VBA Instruction

  50. 2 Using the Object Browserin Word • To view the next level of objects in the object model hierarchy, use the steps on pages 125 to 127 of the textbook • If you are using a color monitor, you will notice that the object model diagram located at the top of the Help screen contains two blue rectangles and one yellow rectangle • You can use this Help screen to display a listing of the properties, methods, and events associated with a Document object

More Related