Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Object Model Visual Basic for Applications 2.

Similar presentations


Presentation on theme: "The Object Model Visual Basic for Applications 2."— Presentation transcript:

1 The Object Model Visual Basic for Applications 2

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

3 Concept Lesson: Understanding the Object Model n 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 n A collection object, typically referred to simply as a collection, is a group of one or more individual objects treated as one unit 2

4 Simplified Object Model for a Library 2

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

6 The Most Commonly Used PowerPoint Objects 2

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

8 Referring to Objects in VBA Code n 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 n 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 n 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 n In most collections, the index of the first object in the collection is 1 2

9 Referring to an Object’s Properties and Methods in VBA Code 2 n A property is a characteristic of an object, and a method is an action that an object can perform on its own n 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 n 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 Referring to an Object’s Properties and Methods in VBA Code n Before you can customize an application, you need to become familiar with the objects in its object model n One way to do so is to use the Object Browser, which is available in the Visual Basic Editor 2

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

12 The Object Browser 2 n 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 n All of the information pertaining to an application’s object model is stored in an object library, which is simply a file

13 The PowerPoint Application Object’s Help Screen 2

14 Getting Help in the Object Browser n 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 n According to the Help screen, the Application object represents the entire Microsoft PowerPoint application n 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 2

15 Getting Help in the Object Browser 2 n 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 n You can view the Help screen for an individual Presentation object by clicking Presentation in the Presentations Collection Object Help screen

16 Presentations Collection Object Help Screen 2

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

18 Using the Immediate Window n The Application.Quit instruction does not begin with a question mark n 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 n The Quit method simply instructs the application to close itself 2

19 Using the Immediate Window n 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 n The Application.Presentations.Count reference indicates that the Count property is a member of the Presentations collection, which is contained in the Application object n 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 2

20 Using the Immediate Window 2

21 Immediate Window Showing Additional VBA Instructions and Their Results 2

22 Repeating and Deleting Instructions in the Immediate Window n 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 2

23 Printing Code from the Immediate Window n 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 n Like the Object Browser and the Help screens, the Immediate window is an invaluable tool for learning an application’s object model 2

24 Summary To refer to an object in an application’s object model: n Specify the object’s exact location in the object model n 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: n Use the expression.property syntax To invoke one of an object’s methods: n Use the expression.method syntax 2

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

26 Summary To use the Immediate window in the Visual Basic Editor: n Open the Immediate window in the Visual Basic Editor by clicking View on the Visual Basic Editor menu bar, and then clicking Immediate window n Enter the instruction in the Immediate window n You can copy and paste instructions from one part of the Immediate window to another n 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 2

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

28 Understanding the Excel Object Model n A Range object in Excel is defined as a cell, a row, a column, or a selection of cells containing one or more contiguous or noncontiguous blocks of cells 2

29 Using the Object Browser in Excel n 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 n To access the Object Browser in Excel, use the steps on pages 102 to 105 of the textbook n A Multiple Objects rectangle represents the next level of objects in the object model hierarchy 2

30 Object Browser Window Showing the Excel Object Library 2

31 Microsoft Visual Basic Help Window 2

32 Using the Object Browser in Excel 2

33 n To view the next level of objects in the object model hierarchy use the steps on pages 106 to 108 of the textbook 2

34 Workbook Object Help Screen 2

35 Using the Immediate Window in Excel n Unlike the Code window, the Immediate window allows you to test a line of code without having to create and run an entire procedure n 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 of the textbook 2

36 QtrSales.xls Workbook 2

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

38 VBA Instructions Entered in the Immediate Window 2

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

40 Additional Instruction Entered in the Immediate Window 2

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

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

43 Word Objects and Collections 2

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

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

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

47 Microsoft Visual Basic Help Window 2

48 Quit Method Help Screen 2

49 Example Showing the Quit Method in a VBA Instruction 2

50 Using the Object Browser in Word n To view the next level of objects in the object model hierarchy, use the steps on pages 125 to 127 of the textbook n 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 n You can use this Help screen to display a listing of the properties, methods, and events associated with a Document object 2

51 Documents Collection Object Help Screen 2

52 Document Object Help Screen 2

53 Using the Immediate Window n You will enter several VBA instructions into the Immediate window in the Visual Basic Editor n Recall that, unlike the Code window, the Immediate window allows you to test a line of code without having to create and run an entire procedure n To open Pat’s document, then save the document and enter several VBA instructions into the Immediate window, use the steps on pages 128 to 132 of the textbook 2

54 Offer Document 2

55 Visual Basic Editor Showing Open Project Explorer and Immediate Windows 2

56 Using the Immediate Window 2 n The first VBA instruction you will enter into the Immediate window, ?Application.Name, will display the value stored in the Application object’s Name property n When you are told to enter an instruction, you always will be shown the complete instruction to enter

57 Using the Immediate Window n To use the Selection object, use the steps on pages 132 and 133 of the textbook 2

58 Using the Range Object’s Select Method 2 n You can use the Range object’s Select method to select a word, sentence, paragraph, or character in a document n To use the Select method in the Offer document, use the steps on page 134 of the textbook

59 Access Lesson: Understanding the Access Object Model n Figure 2-43 shows the portion of the Microsoft Access object model that includes several of the most commonly used Access objects 2

60 Understanding the Access Object Model n In Access, forms and reports are considered AccessObject objects n You will use the Object Browser in the Visual Basic Editor, to display a Help screen for the Application object in Access 2

61 Using the Object Browser in Access n To use the Object Browser in Access, use the steps on pages 140 to 144 of the textbook 2

62 Microsoft Visual Basic Help Window 2

63 Quit Method (Application Object) Help Screen 2 n The syntax of the method is Application.Quit

64 Using the Object Browser in Access 2 n When you enter the Quit method in either the Immediate or Code windows, the Auto List Members list shows that the names of the constants are acQuitPrompt, acQuitSaveAll, and acQuitSaveNone

65 Using the Object Browser in Access 2 n To view the next level of objects in the object model hierarchy, use the steps listed on pages 145 to 146 of the textbook n Notice that a Multiple Objects rectangle, which represents the objects located on the next level in the object model hierarchy, appears below the CurrentProject rectangle in the Help screen

66 AllReports Collection Help Screen 2

67 Using the Immediate Window in Access n You will enter several VBA instructions into the Immediate window in the Visual Basic Editor n To enter several VBA instructions into the Immediate window, use the steps on pages 146 to 149 of the textbook 2

68 VBA Instructions Entered in the Immediate Window 2


Download ppt "The Object Model Visual Basic for Applications 2."

Similar presentations


Ads by Google