Understanding the Microsoft Project Object Model


Now that you have a foundation in Visual Basic concepts and know your way around the development environment, you're almost ready to begin creating customized solutions in Microsoft Project. The final piece of the VBA puzzle to familiarize yourself with is the object model.

But how do you familiarize yourself with an object model? Should you just dive in and start writing code, trusting that you'll figure out the object model as you go? Should you start reading the Help files that are included with Microsoft Project? Or should you use the object browser to (literally) browse the object model? The answer to all three of these questions is Yes. Choose the option that best suits your particular requirements and situation.

What Is an Object Model?

As described in Chapter 30, an object model is a system of objects that provides a programmatic model for interacting with an application. That's a bit of a mouthful, but what it means is that the object model is your portal to the features of Microsoft Project. The features of the object model, in concert with the power of Visual Basic, provide you with the ability to make Microsoft Project do virtually anything you want.

Inside Out: Not everything is in the object model
start example

The object model is amazingly full-featured , but it is important to understand that it's not the Visual Basic "version" of all the features in Microsoft Project. As with every program that exposes one to programming, the object model does not always have a one-to-one correlation to what you can see or do in the user interface. It is just those portions of the program that seem useful for macro writers and other developers. This is why, even if new items have been continually added as the programming environment in Microsoft Project has developed, you can still occasionally find features that are not fully accessible (or not available at all) through the object model.

end example
 

Learning a mature object model like that of Microsoft Project is made easier by relationships, both in the object model and in the Microsoft Project interface:

  • Items that are important in the program are important in the object model, too. For example, the most important objects in the object model, aside from the Application, are the Project, Task, Resource, and Assignment objects.

  • Relationships between items in the object model match those same relationships in the program itself.

  • The hierarchical nature of the object model makes finding these relationships much simpler.

    Note  

    For more information about working with Microsoft Project's object model from another program, see "Extending and Automating Microsoft Project" later in this chapter.

Useful Tools When Learning the Object Model

Although there are many different ways to learn your way around the object model, there are three tools that can make doing so much easier:

Auto List Members .       This option for the Visual Basic Editor (enabled by default) automatically lists all methods , properties, events, and children for an object as you type your code.

Besides saving additional typing ”you can just select the item you want from the list ”this option can help you learn about a relevant portion of the object model as you work (see Figure 31-8).

click to expand
Figure 31-8: The Auto List Members box, shown here displaying part of the list ‚for the Application object, is great way to learn about the object model as you work.
Tip  

How object-oriented code relates to the object model       When you see a period between two object names in Visual Basic code, such as Application.Projects or ActiveCell.Task.Assignments, you have "branched" from one section of the object model to another.

If you think of the object model as a tree with the Application object as its root, this "dot syntax" of object-oriented code is not only a useful reminder that you've moved from one "branch" to another, but it can actually help you understand the relationships between items in the object model.

Microsoft Project Visual Basic Reference.       Whether you use it with the object browser, use it while writing code, or just browse its contents, the Visual Basic reference for Microsoft Project contains everything you need to know about the object model.

There are graphical representations of the object model hierarchy, full explanations of the members of the Microsoft Project object model ”including links to Help topics about the other object models automatically referenced by a project ”and many examples showing the object model "in action."

Object Browser. The object browser (see Figure 31-9) displays all the items (properties, methods, events, and so on) in every object model that is referenced by your project. Open it by pressing F2 or clicking the Object Browser button.

click to expand
Figure 31-9: The object browser displays information about the libraries referenced by your project.

The object browser has three panes. The pane on the left is a list of classes. A class can be one of several things, including an object, collection, module, type (a user-defined data type), or enumerated data type (a collection of constants; usually referred to as an enum ).

Clicking a class displays a list of members for the class in the pane on the right. In the figure just shown, one of the members of the Application object is selected.

start sidebar
Finding the Default Member of a Class

The icons next to a class or member name provide information about the item, such as whether it's a method or a property. A method with the following icon:

or a property with the following icon:

is the default member of its class.

Although default members can be used simply by referring to their class, doing so can sometimes make your code less readable. For example, although CostRateTable(2) is much less readable than CostRateTable.Index(2) because the default property for most objects is Name, using Tasks(4) (or any collection name) instead of Tasks.Item(4) is a common practice.

end sidebar
 

The Details pane at the bottom of the Object Browser (see Figure 31-10) lists specifics about the selected item, such as whether a property is read-only or read/write, the numerical value for a constant, or the arguments for a method.

click to expand
Figure 31-10: For properties or methods that return objects, the name of the object is a link in the Details pane.

It can be useful, especially if several libraries are referenced by your project, to filter the list of items shown in the object browser. You might also want to search for specific items by name or by part of a name. Use the Library and Search Text boxes (see Figure 31-11) to filter the list of items referenced by your project and search for specific items, respectively.


Figure 31-11: Use the Library box to filter items in the object browser and the Search Text box to find specific items.
Note  

By default, the Search Text box performs "loose" searches, which means that searching for task returns items such as Task, TaskDependency, ExternalTask, DefaultTaskType, and so on.

To return only exact matches for a search, right-click anywhere in the Object Browser window and then click Find Whole Word Only.




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