At the top of the object model is the Application object, which provides access to Project 2000 itself. One of the primary roles of the Application object is to provide access to other top-level objects that you will manipulate through code, just as you fire up the Project 2000 application to get to a project’s tasks and resources. The Application object is a very versatile object and in theory, any action in Project 2000 can be accomplished through the use of methods of the Application object. However, using the Application object to manipulate tasks and other items is often the least efficient way to accomplish the job. Furthermore, although you can change the value of any field for an item with the Application object, the Application object provides no methods for examining the current value of a field in an item.
The Project Object
The Project object represents an opened project document. The Projects collection of the Application object contains all project files that are currently open-the same collection of objects you see if you open Project’s Window menu. To create a new Project you can use the Add method of the Projects collection or the File New method of
the Application object.
The Project Object is the other object in Project’s object model that has public events. (fable 25.2 lists the public events of the Project object.) You’ll write event procedures for the Project Object’s events In the This Project module. When you use the This Project module, you do not need to declare and set an object variable as you do for the Application object. The This Project module is already pointed at the current project .
Using the Task Object
The Task object represents a task within a project ..This object allows the fields of a task to be examined or changed pro grammatically. Task objects are not top-level objects, and must be created or accessed by using the Tasks collection of a top-level object. The Tasks collection contains the tasks associated with the collection’s parent object. This is usually a Project object, a Selection object, or a Cell object. To add a new Task to the Parent object, you use the Add method of the Task collection.
Using the Cell and Selection Objects
The Cell object and Selection object represent selected items within the Project application. Neither of these objects belongs to a collection, nor can new instances of the objects be created. To access these objects, properties of the Application object must be used. The Active Cell property of the Application returns a Cell object, and the Active Selection property returns a Selection object.
Project has defined constants for all the Field ID numbers. These constants are the full name of the field, prefixed with pj. The Fi e 1dID constant for Task Actual Work would be pj Task Actual Work. This also works for custom fields, but you must use Project’s actual name for the field (Task l , Cost7, and so on). Thus, the FieldID constant for the Task! custom field would be pjTaskl
The Selection object represents a selected range of cells within the table view. The methods of the Selection object are very similar to the methods of the Cell object. The Tasks property returns a Tasks collection if the selection is in a tasks tab’e. Likewise the Resources property returns a Resources collection if the selection is in a resource table. A selection can include multiple columns in the table, so List objects are used to return the field IDs and field names of the selected columns. The Field IDL; st property is used to retrieve a List of field IDs for the column, In a similar fashion the, Field Names List property is used to retrieve a List of field names. The List object has two main properties:the Count, which returns the number of items in the list and the item property, which returns the value of the item as a string. The following example would display the names of all selected columns in a message box.
Dim pj5ele’tion As MSProject.Selection
Dim pjListNames As MSProject.List
Dim strMsgText As String
Dim i As Integer
, Set pjSelection – Application.ActiveSelection
Set pjListNames – pjSelection.FieldNameList
.” If pjListNames.Count > 0 Then
For i – 1 To pjListNames.Count
strMsgText – strMsgText & & pjListNames(i)
strMsgText – ‘No Cells Selected’
Creating User Forms
A User Form, which is a form module in VBA, should not be confused with a Project 2000 Custom form. Like Outlook 2000, Project 2000 has two types a,f forms, each with its own capabilities and creation methods. It is important to differentiate between the two types. Project 2000 custom forms, created by using Project’s Custom Form Editor, were discussed in Chapter 23. This chapter discusses the second type of form you can use in Project, the UserForm
To create a User Form, just open Project and create a new project. Choose Tools :> Macros > Visual Basic Editor to open the IDE. Choose Insert > User Form from the IDE menu to open a new form
Our form will be used to collect two pieces of information from the user. There are several spreadsheets that include travel information. Users can select any of the spreadsheets to gr~b location data. First, the user will select a location in which an Excel spreadsheet is stored. Second, the user will select a rate per mile traveled. We also need a way for the user to let us know they have finished entering the data,’ or to discard the data they entered. To create our user interface, we’ll use form controls from the Toolbox.
The Control Toolbox is similar to the tool palette of a paint program. When you click a control’s button on the Toolbox, the mouse pointer turns into an icon for that type of control. After you have selected the type-of control you want to use, draw it onto the form as you would draw a shape in a paint program.
To move, resize, or modify the properties of a control, first select the control. The selection tool is used to select one or more controls. If you hold down the Ctrl key, you can select or un select multiple controls, Occasionally, the pointer does not change back to the selection control after you paint a control. If this happens, click on the Selection tool to get your normal mouse pointer back.
The Command Button control should be used for actions that happen immediately, such as canceling a dialog box, opening another form, or printing a report. You use the Click event of the Command Button control to respond to the users’ actions.
The Text Box control provides users with an area where they can edit or input text. You use this control to collect information from the user. The List Box control allows you to display a list of predefined options to the user. This control can be set to accept a single option from the list or allow the user to make multiple selections. L;stBox controls are useful by providing the user with a list of items from which to choose.
The Combo Box control Is a combination of a TextBox and a L; stBox control. When you want users to select one item from a list or enter an Item that doesn’t appear on the list, use a ComboBox.You also can set a ComboBoxto function like a drop-down list. In Dropdown list mode, the ComboBoxfunctions as a L; stBox that allows the user only one choice. Drop-down list boxes are useful on forms that have many controls because hey require less room.
The Label control allows you to place text on the form that a user cannot edit. You use this control to place labels for your TextBbx and other controls. Users are used to labels not doing anything, so you should avoid placing event procedures on labels. The Image control allows you to place images in the form. You can use this control to add your company’s logo to the form or to add an image that represents the purpose of the form.
The Option Button control is used to provide the user with a list of pre-defined options, from which they can select a single option. Option Buttons should be grouped so that a user can select only a single option. There are two ways to create an option group in VB: place Opt; on Button controls in a frame, or assign the same Group Name property to each Option Button control.
Both the Opt; on Button and the Check Box controls have a Group Name property. If you set this property when the user selects a button or clicks on the button, any other buttons in the group are unselected. Although you can set a Group for a CheckBox,you should avoid this. By convention, .Opti onButton controls should be used if the Buttons are grouped and CheckBoxcontrols should be used if the buttons are not grouped.
The Toggle Button control is similar in function to a CheckBox, and provides an alternate graphical appearance to the CheckBox for yes/no controls. Normally, the CheekBpxis used on UserForms for data entry or UserForms that could be paper forms, and Togg1eButtons appear on forms that furiction like control panels.
The Frame control is a container control. It allows you to group controls together. Users are accustomed to frames, so frames make your form more understandable. The more controls you add to an application, the longer it takes to load and the less responsive it is, so some programmers avoid frames.
The Multi Page control is a container control with tabs for each page. Each tab is a separate page that you can place controls on. When the user switches tabs, the control automatically hides the controls from other pages and shows the controls on the active page.
The Tab Strip control allows you to place a row of tabs on a control, similar to those in a property sheet. This control is not a container control, and is normally only used when tabs are used to change the view or filter options of a forms control. Under most circumstances, the Multi Page control will be more useful.
The Spin Button control allows users to select a numeric value. You can set a Min and Max range for the value. You can also set the Small Change property of the control to determine how much the value changes with each click from the user. Thintrol pro Vi”desno indication to the user of its current value, so it is usually paired with another control! such as a label or textbox, to display the value.
The Scroll Bar control is similar to the Spi nButton control, except that it has a graphical slider that indicates the relative position of the control’s current value in its range. In addition to the Min,Maxand Small Change properties, the Seril Bar has aarge Change property, which determines the value change if the user click in the slider region.