Title: Working with the Object Model
1Working with the Object Model
2
- Visual Basic for Applications
2Objectives
2
- In this unit, you will learn how to
- Refer to the objects contained in an
applications 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
3Concept LessonUnderstanding the Object Model
2
- Although all VBA-enabled applications share the
same language, each contains its own unique set
of objects, referred to as the applications
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
4PowerPoint Object Models
- PowerPoint 2000 Object Model
- PowerPoint XP Object Model
- PowerPoint 2003 Object Model
- PowerPoint 2007 Object Model
- ? PowerPoint 2010 Object Model
- PowerPoint 2013 Object Model
5Referring to the Objects Containedin an
Applications Object Model
2
- 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
6The Most Commonly Used PowerPoint Objects
2
Exhibit 2-1 The most commonly used PowerPoint
objects
7Application object
- Represents the entire Microsoft PowerPoint
application. - Contains application-wide settings and options
- Contains properties that return top-level
objects, such as ActivePresentation. - You can use the following properties of the
Application object without the object qualifier - ActivePresentation
- ActiveWindow
- Presentations
8Referring to the Objects Containedin an
Applications Object Model
2
- 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
Exhibit 2-2 The PowerPoint objects and
collections
9Referring to Objectsin VBA Code
2
- You can use VBA to refer to any element in an
applications object model, but to do so you must
specify the objects exact location in the object
models 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 objects 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
10Referring to an Objects Properties and Methods
in VBA Code
2
- 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 objects 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 objects property, and
you use the expression.method syntax to invoke
one of its methods
11Referring to an Objects Properties and Methods
in VBA Code
2
- 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
12The Object Browser
2
- A class is the formal definition of an object it
defines the properties that control the objects
appearance, and the methods and events that
control the objects behavior - All of the information pertaining to an
applications object model is stored in an object
library, which is simply a file
Exhibit 2-3 The Object Browser showing only
PowerPoint classes
13Getting Help in theObject Browser
2
- You can view an objects Help screen by clicking
the objects 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
14Getting Help in theObject Browser
2
- To view a Help screen for one of these objects,
you select the objects name in the list, and
then click the Display button
15Presentations Collection Object Help Screen
2
Exhibit 2-4 The Presentation collection objects
Help screen
16Using the Immediate Window
2
- In addition to the Code window, the Visual Basic
Editor also provides an Immediate window into
which you can enter VBA instructions
Exhibit 2-5 The Immediate window showing the VBA
instructions
17Using the Immediate Window
2
- In the ?Application.Presentations.Count
- Application.Presentations specifies the location
of the Presentations collection in the object
model - Count specifies the desired property
- In the ?Application.Presentations(2).Name
- Applications.Presentations(2) is the expression
- Name is the property whose value you want to
display
18Printing Code from the Immediate Window
2
- You can use either a text editor or a word
processorfor example, WordPad or Wordto 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 applications object model
19Summary
2
- To refer to an object in an applicationsobject
model - Specify the objects exact location in theobject
model - If the object is a member of a collection,
include either the objects name or its index in
a set of parentheses following the collections
name - To access the property of an object
- Use the expression.property syntax
- To invoke one of an objects methods
- Use the expression.method syntax
20Summary
2
- 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
Editors 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
21Summary
2
- 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
22Excel LessonUnderstanding the Excel Object
Model
2
- Exhibit 2-6 shows the portion of the Microsoft
Excel object model that includes the most
commonly used Excel objects
Exhibit 2-6 The Excel Object model
23Understanding the Excel Object Model
2
- 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
Exhibit 2-7 The Excel objects and collections
24Using the ObjectBrowser in Excel
2
- 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, click the
Object Browser button in Visual Basic Editor - A Multiple Objects rectangle representsthe next
level of objects in the objectmodel hierarchy
25Using the ImmediateWindow in Excel
2
- Unlike the Code window, the Immediate window
allows you to test a line of code without having
to create and run anentire procedure
26Referring to Range Objects
2
- A Range object in Excel is defined as a cell, a
row, a column, or a group of contiguous or
noncontiguous cells
Exhibit 2-8 Referring to a Range object in Excel
27Word LessonUnderstanding the Word Object Model
2
- Exhibit 1-9 shows the portion of the Microsoft
Word object model that includes the most commonly
used Word objects
Exhibit 2-9 The Word Object model
28Word objects and collections
2
Exhibit 2-10 The Word objects and collections
29Understanding the Word Object Model
2
- 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 objects name is Word Lesson.doc
30Understanding the Word Object Model
2
- 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
31Using the Immediate Window
2
- You will enter several VBA instructions into the
Immediate window in the Visual Basic Editor - Recall that, unlike the Code window, the
Immediate window allows you to test a lineof
code without having to create and run an entire
procedure
32Using the Range Objects Select Method
2
- You can use the Range objects Select method to
select a word, sentence, paragraph, or character
in a document
33Access LessonUnderstanding the Access Object
Model
2
- Exhibit 2-11 shows the portion of the Microsoft
Access object model that includes several of the
most commonly used Access objects
Exhibit 2-11 The Access Object model
34Understanding the Access Object Model
2
- In Access, forms and reports are considered
AccessObject objects - You will use the Object Browser in the Visual
Basic Editor, to display a Help screen for the
Application object in Access
35Using the Object Browser in Access
2
- 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
36Using the Object Browser in Access
2
- 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
37Using the ImmediateWindow in Access
2
- You will enter several VBA instructions into the
Immediate window in the Visual Basic Editor