Understanding Objects, Properties,Methods, and Events


Understanding Objects, Properties, Methods , and Events

Objects, properties, methods, and events, along with variables and procedures, form the core of how you interact with Microsoft Project, whether it's by using a macro or creating an entire VBA program within Microsoft Project.

Tip  

Standard Visual Basic and general programming terms are italicized the first time they are discussed in this chapter.

Objects

An object is a discrete identifiable thing. It can be something generic, such as a window or a toolbar; or it can be something specific to Microsoft Project, such as a project or a resource. Most objects have at least three common properties , such as the following:

Name      This is the name of the object, such as "project" or "calendar."

Application      This is a way to access the object that represents the software itself.

Parent      This is a way to access the object that precedes the object in the object model .

Objects also usually have properties specific to their types. For example, a Task object has properties that return the start and finish dates. Because objects form the basis of how you interact with Microsoft Project, they also have methods and events .

A collection is a group of objects, almost always of the same type: A Tasks collection, not surprisingly, is made up of Task objects. Collections typically have the following features:

An Add method       This is a way to add a new object to the collection.

An Item property       This is a way to access a particular object in the collection.

A Count property       This is the number of objects in the collection.

Perhaps confusingly, a collection is also an object, which means that a collection can have all the same features of an object, including properties (such as Name, Application, and Parent) and methods.

start sidebar
Parents and Children: The Object Model Hierarchy

An object model is a system of objects that provides a programmatic model for interacting with an application. Object models in VBA are represented in a hierarchical "tree" fashion with the Application object (the object representing the program itself) as the root. All other objects and collections are children of the root object.

In Microsoft Project, for example, collections such as the Windows, Projects, and Tables collections ”as well as the Cell and Selection objects (and others) ”are child objects of the Application object. For each of those child objects, then, the Application object is not only the root of the object model, but the parent object .

Other than the Application object, which has no parent, every object and collection has a parent object. Objects usually have child objects (the Project object has Resources and Tasks collections, for example), and collections always have children: Each object in the collection is a child of the collection itself.

end sidebar
 

Properties

A property is an aspect or characteristic of an object. A property can be simple, such as the name of the object. A more complex property might be a collection of child objects. Properties are either read-only , which means that they only provide information; or read/write , which means they can also be changed as needed.

For example, the Name property of a Project object is read/write because you can always rename a project. The Parent property, though, which returns a reference to the Application object, is read-only.

Methods

A method is just a way of telling an object to do something. The Application object, for example, has an AppMinimize method. When you call (or "invoke") the AppMinimize method, you're telling Microsoft Project, represented by the Application object, to minimize its window.

Many methods return information about the results of the action, such as when the AppMinimize method returns a value of True or False to tell you whether Microsoft Project was minimized successfully.

Some methods can accept arguments , which are ways of supplying additional information to the method. The Tasks collection's Add method, for example, has optional Name and Before arguments, so that you can specify a name and position for the new task as you create it. For example, the following code creates a task called "Write chapter" as the third task (the new task is inserted before what was the third task) in a collection of tasks:

 ActiveProject.Tasks.Add "Write chapter", 3 
start sidebar
Calling Methods That Return Values

If a method returns a value, you can call the method with parentheses or without them:

  • Calling a method with parentheses returns either a value or a reference to an object, so you must do something with the result or an error will occur. The following code returns a reference to the Task object it just created:

     ActiveProject.Tasks.Add("Write chapter", 3) 

    Without additional code to work with the returned object, this code will result in a syntax error.

  • Calling a method without parentheses means that the method takes action and discards the return value. The following code creates a task:

     ActiveProject.Tasks.Add "Write chapter", 3 
Cross-References  

For more information about methods that return values ( functions ), see "Understanding Procedures" later in this chapter.

end sidebar
 
Cross-References  

For more information about working with optional arguments, see "Named Arguments" later in this chapter.

Events

An event can occur ( fire ) when an object does something as the result of a method or user action, which can trigger the event. Sometimes, several events can fire in sequence. Events can contain calls to methods, which can then cause even more events to fire.

For example, when the following code creates a new task, it triggers the Application object's ProjectBeforeTaskNew event:

 ActiveProject.Tasks.Add "Write chapter", 3 

If you want to prevent the task from being created, you can use the event's Cancel argument to do so.

In another example, closing Microsoft Project with an unsaved project file open causes several events of the Project object to fire:

Project_BeforeSave.       This fires just before Microsoft Project asks if you want to save the project.

Project_BeforeClose.       This fires just before the project closes .

Project_Deactivate.       This fires as the closing project is about to lose focus .

Note  

Several Application-level events also fire in this example.




Microsoft Office Project 2003 Inside Out
Microsoft Office Project 2003 Inside Out
ISBN: 0735619581
EAN: 2147483647
Year: 2003
Pages: 268

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net