In my earliest post, you learnt about how to use Calculate Event procedure in the Project object to color tasks with no dependencies set. The events of Project are available for any open project – reference http://msdn.microsoft.com/en-us/library/office/dn217671(v=office.15).aspx
You have a more powerful object called Application. It represents the entire Project application. To write event procedures for the Application object, you must follow a new approach.
Let’s take an example to illustrate Application object’s event procedure. As a best practice, it is advisable not to delete baseline tasks since you will lose the evidence and will have incorrect variance. It is hence recommended to Inactivate the task (see this blog about the benefits).
Design and Build tasks is approved and Test task is inserted later.
After deleting Build task, you lost the proof that Build was part of your plan
The following steps will guide you how to create event handler before a task is deleted
1. Click View tab. Click Macros dropdown and select Visual Basic
4. You must create a new object using the WithEvents keyword in a class module
Public WithEvents App As Application
5. Now change the object and procedure to "App" and "ProjectBeforeTaskDelete". ProjectBeforeTaskDelete event will be fired for every task that is deleted.
6. Within this sub-routine include the below code
7. You need to bind the current Application object to the App object declared in the class to fire the events. Open the ThisProject module and paste the below code. This code declares an object X of type Events (the class module we created earlier), and sets the current Application object to the App object of Events class module
The code displays the message "Dont’ delete baseline task. Instead Inactivate the task" and cancels the delete action if the Baseline Start of the task deleted is set.