Project Change Event

M

Marguerite

I am currently working on a program with 6 project managers. There is one
plan (which I inherited). Each PM makes a local copy and uses Text5 to
identify the task to be added changed or deleted. Well, they are not
updating Text5 consistently. So I would like to set an event to identify
when a task is changed or a task is added. In reading the documentation, it
said that you had to define a new class module and class in order to "listen
for" project events.

I created a class module called EventClassModule with the following code:

Option Explicit
Option Base 1

Public WithEvents App As Application
Public WithEvents Proj As Project

Dim NewTaskIDs() As Integer
Dim NumNewTasks As Integer

Dim ProjTaskNew As Boolean

Private Sub App_ProjectTaskNew(ByVal pj As Project, ByVal ID As Long)
NumNewTasks = NumNewTasks + 1

If ProjTaskNew Then
ReDim Preserve NewTaskIDs(NumNewTasks) As Integer
Else
ReDim NewTaskIDs(NumNewTasks) As Integer
End If

NewTaskIDs(NumNewTasks) = ID

ProjTaskNew = True
End Sub

Private Sub Proj_Change(ByVal pj As Project)
Dim NewTaskID As Variant

If ProjTaskNew Then
For Each NewTaskID In NewTaskIDs
MsgBox "New Task Name: " &
ActiveProject.Tasks.UniqueID(NewTaskID).Name
ActiveProject.Tasks.UniqueID(NewTaskID).Text5 = "Add"
Next NewTaskID

NumNewTasks = 0

ProjTaskNew = False
Else
ActiveProject.Tasks.UniqueID(NewTaskID).Text5 = "Change"
End If
End Sub

Then in a program module, I created the following code:

Option Explicit
Dim X As New EventClassModule

Sub Initialize_App()
Set X.App = MSProject.Application
Set X.Proj = Application.ActiveProject
End Sub

But it doesn't work. What am I doing wrong?

Any help would be greatly appreciated.
 
B

Brian K - Project MVP

Marguerite said:
I am currently working on a program with 6 project managers. There is one
plan (which I inherited). Each PM makes a local copy and uses Text5 to
identify the task to be added changed or deleted. Well, they are not
updating Text5 consistently. So I would like to set an event to identify
when a task is changed or a task is added. In reading the documentation,
it
said that you had to define a new class module and class in order to
"listen
for" project events.

I created a class module called EventClassModule with the following code:

Option Explicit
Option Base 1

Public WithEvents App As Application
Public WithEvents Proj As Project

Dim NewTaskIDs() As Integer
Dim NumNewTasks As Integer

Dim ProjTaskNew As Boolean

Private Sub App_ProjectTaskNew(ByVal pj As Project, ByVal ID As Long)
NumNewTasks = NumNewTasks + 1

If ProjTaskNew Then
ReDim Preserve NewTaskIDs(NumNewTasks) As Integer
Else
ReDim NewTaskIDs(NumNewTasks) As Integer
End If

NewTaskIDs(NumNewTasks) = ID

ProjTaskNew = True
End Sub

Private Sub Proj_Change(ByVal pj As Project)
Dim NewTaskID As Variant

If ProjTaskNew Then
For Each NewTaskID In NewTaskIDs
MsgBox "New Task Name: " &
ActiveProject.Tasks.UniqueID(NewTaskID).Name
ActiveProject.Tasks.UniqueID(NewTaskID).Text5 = "Add"
Next NewTaskID

NumNewTasks = 0

ProjTaskNew = False
Else
ActiveProject.Tasks.UniqueID(NewTaskID).Text5 = "Change"
End If
End Sub

Then in a program module, I created the following code:

Option Explicit
Dim X As New EventClassModule

Sub Initialize_App()
Set X.App = MSProject.Application
Set X.Proj = Application.ActiveProject
End Sub

But it doesn't work. What am I doing wrong?

Any help would be greatly appreciated.

When y ou say it does not work what do you mean? What does not happen?
Does anything happen?

It seems like you are going a long way around. I would just have the
TaskAdd event an then do the BeforeTaskChange app event and have them just
write directly into the field for a change or addition. you are using the
ProjChange event which does not give you any view into which task changed.
the before task change event works just like the TaskAdd event but gives
you a hook to the task that changed, the field that changed and the old
and new values. So with that you could have the Text5 field (by the way
trap a change to the Text5 field to ensure that the code in the event does
not start a never ending loop LOL) read what field changed. This would let
you get rid of all that code that is trying to capture IDs of new tasks
and such. Have the TaskAdd event get the new ID and just insert ADDED into
the Text 5 field right there in the event rather than setting a boolean
flag and then having the change event write it.

I hope this helps.
 
M

Marguerite

Brian, on your points:
1. When I say it doesn't work, I mean that I run the Initialize_App and get
no errors, but when I add a new task, the msgbox doesn't display. Also, when
I put a breakpoint in the App_ProjectTaskNew, no break occurs.
2. Good point on the endless loop, I have added code to check for Text5
being empty.
3. I want this to work on events because the PM's are less than consistent
in remembering to set the field. With a project of almost 1800 lines, it is
impossible to know what has changed without this field being set.
4. I tried to look up the TaskAdd event and couldn't find it. There were no
events beginning with the letter T in my documentation. The code I used was
all I could find on this topic.

Could you give me some further direction? Thanks.
 
B

Brian K - Project MVP

Marguerite said:
Brian, on your points:
1. When I say it doesn't work, I mean that I run the Initialize_App and
get
no errors, but when I add a new task, the msgbox doesn't display. Also,
when
I put a breakpoint in the App_ProjectTaskNew, no break occurs.
2. Good point on the endless loop, I have added code to check for Text5
being empty.
3. I want this to work on events because the PM's are less than consistent
in remembering to set the field. With a project of almost 1800 lines, it
is
impossible to know what has changed without this field being set.
4. I tried to look up the TaskAdd event and couldn't find it. There were
no
events beginning with the letter T in my documentation. The code I used
was
all I could find on this topic.

Could you give me some further direction? Thanks.

Sorry I meant the tasknew event you are already using. :) Use that one
and the BeforeTaskChange event. Then you dont have to use the Project
Change event and track all those ID numbers between subs.
 
B

Brian K - Project MVP

Marguerite said:
Brian, on your points:
1. When I say it doesn't work, I mean that I run the Initialize_App and
get
no errors, but when I add a new task, the msgbox doesn't display. Also,
when
I put a breakpoint in the App_ProjectTaskNew, no break occurs.
2. Good point on the endless loop, I have added code to check for Text5
being empty.
3. I want this to work on events because the PM's are less than consistent
in remembering to set the field. With a project of almost 1800 lines, it
is
impossible to know what has changed without this field being set.
4. I tried to look up the TaskAdd event and couldn't find it. There were
no
events beginning with the letter T in my documentation. The code I used
was
all I could find on this topic.

Could you give me some further direction? Thanks.

Oh and also look at the Compare Projects utility. it takes two projects
and generates a delta report showing which tasks have been added, removed
or changed. It has a toolbar in Project 2003.
 
M

Marguerite

Brian, thanks for your help and your patience. I did try the compare
utility. It is pretty neat. I even tried to use it for my updates today,
but one of the PM's didn 't pull the latest copy of the master, and so some
of the "changes" were not changes.

However, I did get my events to fire - don't ask me what I did.

One final question, I have the initialize subroutine executing upon project
open. How do I turn it off? I want it on for the PM's and to be able to
turn it off when I get in the plan.
 
B

Brian K - Project MVP

Marguerite said:
One final question, I have the initialize subroutine executing upon project
open. How do I turn it off? I want it on for the PM's and to be able to
turn it off when I get in the plan.

you dont that I know of. It is either on or off. I suppose you could have
the code check to see who the current project user is and NOT do it's
stuff while it is YOU. just a thought.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top