the object model. you can think of the contents of an excel application as a hierarchy of...

19
The Object Model

Upload: aubrey-brown

Post on 14-Jan-2016

220 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

The Object Model

Page 2: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

The Object Model

• You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code

• Each object can contain collections of other objects, and may also have properties and methods

• We’ll illustrate these concepts using the Index List Box demo

Page 3: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

The Listbox Object

• A listbox is an object that contains other objects, such as the items

• It has properties, such as Left (horizontal position of left upper corner), Height, Visible, and others

• It has methods, such as AddItem and Clear• It has events for which there are built-in

functions

Page 4: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

The Object Browser

• VBA has an object browser that will show you the methods, properties, and events associated with each class of objects

• You access the object browser using a button in the VBA toolbar

• This is extremely useful for figuring out exactly what you can do with each type of object, and how to do it

Page 5: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

The object browser button in Windows (or use F2)

Showing the members of ListBox

Members can be events, properties, or methods

Page 6: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

“Locked” is a property with a Boolean value we can set using code or in the Properties window

(Note ListBox is a member of MSForms)

Page 7: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

“AddItem” is a method we can use to add items to the list box.

Page 8: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

“Keypress” is an event for which we can write an event handler

Page 9: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

Mac version: use the view menu

Page 10: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

I searched for listbox…

Members of listbox

Page 11: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

Names and References

• Excel uses a dot to navigate down the object hierarchy, and also to refer to properties, events and methods

• Suppose I have a listbox called lstWord in a user form called frmIndexListBox in a workbook called IndexListBox

• Now suppose I want to refer to this listbox in my code

Page 12: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

The Importance of Being Active

• If I know that at the time my code runs, the user form will be active, then I can just refer to the list box as lstWord, as we have been doing

• If not, but I know the workbook will be active, I would call it frmIndexListBox.lstWord

• Alternatively, the user form has an Activate method I can use when I enter the code to make sure it’s the active form

Page 13: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

In General…

• The complete name of an object starts with the top of the hierarchy (Application) and works its way down, with a dot separating the name of each level

• You don’t have to use the complete name if you know the containing object will be active

Page 14: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

Object Model Hierarchy

• The top level, or root object, is Excel; in the object hierarchy, it is called the Application

• The Application contains a Workbook (maybe several)

• The Workbook contains a Worksheet (or several) and possibly one or more standalone charts

• Worksheets contain things like ranges, cells, and chart objects

• User forms are members of VBA Projects, which also contain Workbooks

Page 15: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

Caveat

• The object structure suggests that Excel and VBA follow a paradigm known as “Object Oriented”

• This is only partly the case. If you are familiar with object oriented programming, you will find that some things work as you expect, and some don’t (like inheritance)

Page 16: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

The Code Follows the Model

• We assume here that the Application is Excel, so it doesn’t get mentioned

• To add an item to the lstWord listbox, you would write the following, where newWord is a string variable, and Additem is a listbox method that takes one argument

frmIndexListBox.lstWord.AddItem (newWord)

• If we know the form is active we just uselstWord.Additem(newWord)

Page 17: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

Using the Object Model (1)

• To refer to anything in Excel, you are implicitly using the object model hierarchy

• Lots of times the top levels can be omitted, because they are the active objects

• You can use Activate methods to make workbooks, worksheets, charts, etc active if you are not in a context where the user would have had to select them

Page 18: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

Using the Object Model (2)

In our earlier example,frmIndexListBox.lstWord.AddItem (newWord)

dots are used to go to the next level of the object hierarchy. The final dot (arrow) is different: it gets us to a method of the listbox object

Page 19: The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can

Help Feature

• If you are writing some code and write the name of an object followed by a dot, the VBA system usually shows you a list of all the properties and methods that could follow the dot; it also shows the names of any objects, such as buttons for a userform, that could follow the dot

• Walkenbach recommends typing the object name into the immediate window at the bottom of the editor and putting the cursor anywhere in the name, then clicking F1. This gives you the help entry for that object.

• (Or of course you can use the F2 key to explore the object model)