An event procedure is tied to an object event (such as a button’s click event), and automatically runs when that event occurs, The Format_Durati on macro is a general procedure. The code isn’t tied to any particular button or control, and runs only on demand. General procedures are very useful. You can fire this macro by assigning it to a tool bar button, a command button, the macro menu, a menu bar, or all of the above.
To create a general procedure without the Macro Recorder, follow these steps:
1. Insert a new module (Insert> Module) in the project or open an existing module.
2. Choose Insert> Procedure from the menu to open the Add Procedure dialog box, shown in Figure 24.S.
3. Enter a name for the procedure in the text box. The naming convention for procedures is to use the prefix.
4. Choose the Type of procedure you’re subroutine, function, or property.
5. Select a scope. The Scope is a procedure’s range: procedures can be called only by other procedures in the same module; Public procedures can be called by any procedure in the same application.
6. Click OK to add the procedure’s snippet to the Code window.
Begin documenting your procedure. Type an apostrophe, hit the space bar, and then enter at least your name and the current date. Feel free to add more descriptive information if you wish, beginning each line with an apostrophe. In the next section, we’ll begin entering code to create a procedure.
Getting Help with VBA and Project 2000 Objects
When you’ enter code, you need to know which objects are available k, and what properties and methods the object has. For a list of objects, use the VBA libraries, which are just a click away in the Object Browser. Click the Object Browser button (or choose View >- Object Browser) to open the browser, shown here
MASTERING TROUBLESHOOTING CONTINUED
The library for the current application should already be selected, but you can choose other libraries from the Project/Library drop-down list To search for an object, enter part or all of the object’s name in the Search Text box and click the Search button. When you choose an object in the Class pane, you’ll see a list of properties and methods for the object in the right (Member) pane of the browser. If you want more information about the selected object, click the Help button in the Object Browser
for a more global view of application objects, browse the object model, the map of all the objects in an application. The model shows each object and how it relates hierarchically to other objects “in the application. To access the object model for an application; open the application, launch Help, and search on the word object. Select any object or collection in the model to get more information on the object, including samples of code using the object
The model uses different colors for objects and.collections, which are groups of objects. For example, there is a collection of paragraphs in a document and a collection of documents in an application. Collections are important application features from a programming point of view. You can iterate through a collection if you want to count the number of paragraphs or save all open documents, but you also have the ability to affect one item in the collection. Select any object or collection in the model to get more information on the object, including samples of code using the object
We will now create a simple procedure to format the bars of a Gantt chart for a Milestone task to be blue rather than their default color of black. We will then use this new bar style when a Milestone is Overdue.
“First, invoke the Object Browser by clicking the Object Browser button, choosing View >- Object Browser, or pressing F2. In order to find exactly which method or command to use to like this change, we will use the search function. See Figure 24.6 for an example of the Object Browser with the search function invoked. Because we know we want to modify the display of a Gantt chart, we’ll type Gantt in the search box and click the Find button
The results of this search include the function Gantt Bar Style Edit. We will use this function to create a new Gantt Bar Style. This function has a lot of arguments. Select the Gantt Bar Style Edit function and click the Help button to open the VBAHelp page for this function. Briefly, the syntax for GanttBarStyleEdit is this:
Now that the Overdue Milestone Gantt Bar Style exists, you can assign it to all milestones in the Gantt chart that are overdue. A Milestone is overdue if it is not completed and the Finish Date is less than (prior to) today. If these two conditions are true, you will use the Gantt Bar Format function to assign the Overdue Milestone Gantt Bar Style to your mlleslones.
,If Task.Milestone – Yes Then
If Task.ActualFinish > Today Or Empty Then GanttBarFormat GanttStyle:-15
End If End