Application.ProjectTaskNew Event

Project Developer Reference

Occurs when a new task is created.

In Microsoft Office Project 2007, you can listen to project-level events from outside of Microsoft Visual Basic for Applications (VBA).

.

Syntax

expression.ProjectTaskNew(pj, ID)

expression   A variable that represents an Application object.

Parameters

Name Required/Optional Data Type Description
pj Required Project The project where the task was created.
ID Required Long The ID of the task that was just created.

Return Value
nothing

Example

The following example shows how the ProjectTaskNew event can be used to listen to project-level events (in this case, the Change event). The same can also be applied to ProjectResourceNew and ProjectAssignmentNew events as well.

  1. Create a new class module called "EventClassModule." Insert the following code:
    Visual Basic for Applications
    
    

    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
        Next NewTaskID
    
        NumNewTasks = 0
    
        ProjTaskNew = False  
    End If
    

    End Sub

    2. In a separate module, insert the following code:
    Visual Basic for Applications
      Option Explicit
    

    Dim X As New EventClassModule

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

    3. Run the Initialize\_App procedure to start listening to the events. 4. Create a new task, and the event causes a message box to pop up every time a new task is added.

    See Also