Working with Events

Macros require user action to start, but you might occasionally need code to start automatically. For example, every time a shared plan is saved, you might want to store information about the person who saved it. In a situation like that, you need code that is run from an event procedure.

An event procedure is any code that is tied to an event, such as when a plan is saved or a task is deleted. When the event fires, the code is activated. Anything you can do in a "regular" procedure you can also do in an event procedure, including calling other procedures and making references to other object models.

For more information about events in UserForms, such as Click events for buttons, see "Creating UserForms" later in this chapter on page 926.

In Microsoft Project, there are events attached to the Application and Project (representing an open plan) objects (see Figure 31-14). You can find the events by looking for the event icon in the object browser or by expanding the Events item in the Contents tab of the Microsoft Visual Basic Reference.

I Classes

Members of Application'

| £ <globals>

Al

AnswerWizard

KBJ Application

il

AppExecute

Assignment

Application

Assignments

f ApplicationBeforeClose

Availabilities

App Maximize

Availability

AppMinimize

Figure 31-14. The ApplicationBeforeClose event is a member of the Application object.

Note Sort items by type

To sort items in the object browser by type instead of simply alphabetically, right-click anywhere in the object browser and then click Group Members.

Creating Application Event Procedures

Creating Application event procedures is a three-step process. The first step is to create a class module that contains an Application object variable declared with events. WithEvents is a Visual Basic keyword that means the variable should respond to events. You can only use WithEvents in class modules.

The second step is to bind the class containing your event procedure to the Application object. You do this by first declaring a variable that represents the class module and then writing a procedure that assigns the variable to the Application object.

For the last step, you need to decide how to run the code that binds the class to the Application object. You can either write the binding code directly in a project-level event procedure or if you want to keep your code more modular, you can write it anywhere you like and then simply call it from a project-level event.

Follow these steps to create an event procedure for the Application object:

1 Right-click anywhere in the Project Explorer and then click Insert, Class Module.

2 In the Code window for the new class, enter the following code:

Public WithEvents objMSProjectApp As Application

Note The variable represented here by objMSProjectApp can be named anything you like.

3 In the Objects box, select objMSProjectApp.

The editor creates a blank procedure for the NewProject event. If you want to write a procedure for a different event, click it in the Procedures/Events box.

4 Write your event code as you would for any other procedure.

For example, the following code prevents a particular resource from being assigned to tasks. (Note that the procedure declaration must be entered on one line in the Visual Basic Editor, not on three as shown here.)

Private Sub objMSProjectApp_ProjectBeforeAssignmentChange(ByVal asg As Assignment, _

ByVal Field As PjAssignmentField, ByVal NewVal As Variant, _ Cancel As Boolean)

If Field = pjAssignmentResourceName And _ NewVal = "Patricia Doyle" Then

MsgBox "Patricia is no longer available for assignment!" Cancel = True End If End Sub

5 Change the Name field in the Properties window for the class from Class1 to clsAppEventProcedures. h

6 In any module except the class module in which you wrote the event procedure, enter ®

the following code in the Code window: J2

Dim clsAppEvents As New clsAppEventProcedures

Sub BindEventToApplication()

Set clsAppEvents.objMSProjectApp = Application End Sub

7 Double-click ThisProject in the Project Explorer to open the Code window, click Project in the Objects box, and then type the following code in the Project object's Open event:

BindEventToApplication

Creating Project Event Procedures

To create an event procedure for the Project object, the code must be written in the ThisProject object, rather than in a code module. Events for the Project object are attached to the plan, which ThisProject represents.

Follow these steps to create an event procedure for the Project object:

1 Double-click ThisProject in the Project Explorer to open the Code window.

2 In the Objects box, click Project.

The editor creates a blank procedure for the Open event. If you want to write a procedure for a different event, click it in the Procedures/Events box.

3 Write your event code as you would for any other procedure.

For example, the following code adds information to the plan's summary task about who saved the plan, and when, each time it is saved:

Private Sub Project_BeforeSave(ByVal pj As Project) Dim strName As String strAlias = InputBox("Please enter your name: ") pj.ProjectSummaryTask.AppendNotes vbCrLf & "Saved by " & _ strName & " on " & Date$ & " at " & Time$ & "." End Sub

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment