1 / 19

The Object Model

The Object Model. 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

feoras
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. The Object Model

  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 contain collections of other objects, and may also have properties and methods • We’ll illustrate these concepts using the Index List Box demo

  3. 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

  4. 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

  5. The object browser button in Windows (or use F2) Showing the members of ListBox Members can be events, properties, or methods

  6. “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)

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

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

  9. Mac version: use the view menu

  10. I searched for listbox… Members of listbox

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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)

  16. 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 lstWordlistbox, 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 use lstWord.Additem(newWord)

  17. 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

  18. 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

  19. 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)

More Related