Skip to main content
The World's Best Known Brand for Microsoft Project and Project Server Expertise

Project Server Experts Community Site

Go Search
Project Server Experts Community Site
  
Project Server Experts Community Site > Project Server FAQ KnowledgeBase > UsingApplicationEvents  

Web Part Page Title Bar image
Using Application Events such as ProjectBeforeTaskChange and ProjectBeforeSaveBaseline

You May Have Been Automatically Redirected to This Page, Which Has a New Address (URL). Please Update Your Bookmarks / Favorites Accordingly.

Background Information

Project level events are a very useful tool to automate Project, but they are very limited. The following events are available in the Project object:

BeforeClose

BeforePrint

BeforeSave

Calculate

Change

Deactivate

Open

If you want to write code that validates a value before updating, you cannot do it using the Project events. The change event runs after the change has occurred. With a little additional work, we can add Application events that meet our needs, such as BeforeTaskUpdate. The following events are available in the Application object:

ApplicationBeforeClose

LoadWebPage

NewProject

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

WindowActivate

WindowBeforeViewChange

WindowDeactivate

WindowGoalAreaChange

WindowSelectionChange

WindowSidepaneDisplayChange

WindowSidepaneTaskChange

WindowViewChange

WorkpaneDisplayChange

Resolution

1. In Project’s Microsoft Visual Basic window, click Insert - Class Module.

2. Rename the module to “AppEvents” by selecting the new class module and changing the name in the Properties window. (If the Properties window is not visible, you can turn it on in the View menu.)

3. Add the line of code “Public WithEvents ProjApp as Application” to the class module. This declares the class as a project application and the application can trigger events.

4. Change the object to ProjApp from (General) in the top left dropdown.

5. Select the application event in the top right dropdown. For example, ProjApp_ProjectBeforeTaskChange.

6. Add code to the procedure. For example, the following code alerts the user if they are using a poor task naming convention by using the word “task” in the task’s name.

Private Sub ProjApp_ProjectBeforeTaskChange(ByVal tsk As Task, _

    ByVal Field As PjField, ByVal NewVal As Variant, Cancel As Boolean)

Dim intResult As Integer

If Field = pjTaskName Then

    If InStr(1, UCase(NewVal), "TASK") > 0 Then

        intResult = MsgBox( _

            "You used the word ""TASK"" in the task's name.  Continue?", vbOKCancel, "Task Audit")

        If intResult = vbCancel Then

            Cancel = True

        End If

    End If

End If

End Sub

7. Change to the appropriate code module, such as ThisProject(MyProject), by double-clicking the module in the Project Explorer window (usually in the top left).

8. Add “Dim myApp As New AppEvents” to the top of the module. This references the class and its events.

9. Set the myApp variable’s ProjApp property equal to the project application object by using the following code:

Private Sub Project_Open(ByVal pj As Project)

    Set myApp.ProjApp = Application

End Sub

10. You can add the above code to the Global.mpt or the Enterprise Global instead of the project plan. This code must run prior to using the events. You may have to run the code manually at the beginning of testing since the open event will not run until you save, close, and open the project plan.

11. Test your code.