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

Project Server Experts Community Site

Search
Project Server Experts Community Site
Project Server Help Blog
MSProject Experts
Training Schedule
Contact Us
  
Project Server Experts Community Site > Project Server FAQ KnowledgeBase > UsingApplicationEvents  


Using Application Events such as ProjectBeforeTaskChange and ProjectBeforeSaveBaseline



Microsoft Project Server Frequently Asked Questions (FAQs): Overview



Microsoft Project Server Frequently Asked Questions (FAQs): Details

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.

 

 

Stay Connected

Join Our
LinkedIn Network
Follow Us
on Twitter

About FAQs

Microsoft Project Server Frequently Asked Questions (FAQs) provide information about Microsoft Project Server 2002, Microsoft Project Server 2003, Microsoft Project Server 2007, and Microsoft Project Server 2010.

Topics include common errors and the Project Server queue, project manager and resource manager approvals, enterprise calendars, enterprise custom fields, enterprise projects and the enterprise resource pool, user access, reports and OLAP analysis, resource work, project costing, task progress reporting and timesheets, Project Web Access (PWA) and Project Professional, check-out check-in and the local cache, saving and publishing, and issues risks deliverables documents and project workspaces.

About This Site

Project Server Experts (www.projectserverexperts.com) is an online community that provides answers to Project Server Frequently Asked Questions, allows members of the Project Server community to connect with one another by uploading their Business Cards, provides links to online Project Server resources, and links to Microsoft Project and Project Server related jobs.

This site is brought to you by MSProjectExperts. If you find the information posted here useful, please consider visiting our commercial site (www.msprojectexperts.com) for Project Server books, Project Server training and Project Server consulting services. Please support our sponsors. Thanks. -- The MSProjectExperts Team

Submissions

We welcome content submissions. We give author credit and cross link on all accepted articles. Please send your articles and suggestions to info(at)msprojectexperts.com.

Copyright

Reproduction of content on this site is strictly prohibited without the express permission of MSProjectExperts. Copyright (c) 2011 CHEFETZ LLC. All rights reserved