Application Events>

using application events requires a few steps. The first step is to create a new class module and declare an object of type Application with events.
Creating the class module is done by going to the insert menu and selecting "ClassModule"




When you have done this, double click on the class module and declare the object by using the following code:

Public WithEvents App As Application

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, _
ByVal Field As PjField, _
ByVal NewVal As Variant, _
Cancel As Boolean)

If Field = pjTaskName And NewVal = "foo" Then
MsgBox ("you can not change the name to foo")
MsgBox ("The old name was " & t.Name)
Cancel = True
End If

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 "App") in the class module with the Application object.
It sounds complicated, but it is really rather simple. First we declare a new object based on the class module. In this case our class module is named TEvent.
Code to do this would be something like:
Dim X As New TEvent

Now that we have this object we need to initialize it. Basically we need to tell it what the "App" is.

We do this with the following code:
Set X.App = Application

After you run the InitializeApp procedure, the App object in the class module points to the Microsoft Project Application object, and the event procedures in the class module will run when the events occur.
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

Comments

for more information
http://zo-d.com/blog/archives/programming.html

Popular posts from this blog