Working With Events
Often one wants Project to do something when something changes in the project
file. An example of this is having some sort of macro run when the project is
opened or when it is saved. Project offers a number of Project events which
allow this. They include:
Project_Open (which acts like the On_Open event you may be familiar with), Activate, BeforeClose, BeforeSave, Calculate, Change, and Deactivate
These events apply to the project and are fairly simple to implement. There are also a number of application level events which allow you to specify actions based on changes in individual fields. They are somewhat more difficult to implement, but if you can follow the example below you will have no problems.
The list of application events includes:ProjectAfterSave, ProjectAssignmentNew, ProjectBeforeAssignmentChange, ProjectBeforeAssignmentChange2, ProjectBeforeAssignmentDelete, ProjectBeforeAssignmentDelete2,
ProjectBeforeAssignmentNew, ProjectBeforeAssignmentNew2, ProjectBeforeClearBaseline,
ProjectBeforeClose, ProjectBeforeClose2, ProjectBeforePrint, ProjectBeforePrint2,
ProjectBeforeResourceChange, ProjectBeforeResourceChange2, ProjectBeforeResourceDelete,
ProjectBeforeResourceDelete2, ProjectBeforeResourceNew, ProjectBeforeResourceNew2,
ProjectBeforeSave, ProjectBeforeSave2, ProjectBeforeSaveBaseline, ProjectBeforeTaskChange,
ProjectBeforeTaskChange2, ProjectBeforeTaskDelete, ProjectBeforeTaskDelete2, ProjectBeforeTaskNew,
ProjectBeforeTaskNew2, ProjectCalculate, ProjectResourceNew, ProjectTaskNew, NewProject, LoadWebPage, ApplicationBeforeClose.
| Using a Project Event: | |
|---|---|
|
This is the simplest form of event. Open your project file. Hit the ALT+F11 keys to open the visual basic editor. In the upper left you will see a window with a typical windows tree view.
Click on the + signs until the project it expanded and you see the "ThisProject" object. It should look like the diagram on the right, although with a different
project name.
|
|
| Application Events: | |
|
The example above only requires pasting some code in a single place. However using application events requires a few more steps.
The first step is to create a new class module and declare an object of type Application with events.
|
|
| After the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for the new object. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.) Writing the procedure is similar to writing any macro. The image below shows a simple example using the ProjectBeforeTaskChange event. |
|
|
|
Note that NewVal holds the value that the user has input. The original value can still be referenced in the standard way (t.Name). The code to cut and paste is shown next. |
Public WithEvents App As Application
Private Sub App_ProjectBeforeTaskChange(ByVal t As Task, _ |
Note that a space followed with an underscore is used to break a single line of code. This is called a line continuation and I use it to keep code readable when there is a long line. |
|
Now that you have written the code, there is one final step to undertake before using it.
Before the procedures will run you must connect the declared object (in this case the one we called
Now that we have this object we need to initialize it. Basically we need
to tell it what the
We do this with the following code:
Most of the time you will want to do the initalization when your project opens so the events will work from the start, but you can put this information in any typical module which holds some of your macros. The screenshot below shows what it would look like if we want it to initialize when the project file opens. |
|
This example shows how events can be in a specific project file. You can also have this code in the global.mpt file so that things occur whenever you open Project as an application. The Project_Open event is also useful to distribute macros or other standard formatting. For example, you could have a Project_Open macro which sets up an environment for you (copying views tables etc.) using the organizer. When a user opens the file, those items could be copied into their global.mpt file. |