I send, with a macro in MS Project, an Outlook email when a resource is assigned.
I want to notify only the resources that have been assigned since the last time the macro was run.
This code emails every assignee the task they are assigned every time it is run.
Dim oProject As Project
Dim oTask As Task
Dim oResource As Resource
Dim oMail As Object
' Get current project
Set oProject = ActiveProject
' Loop through each task in the project
For Each oTask In oProject.Tasks
' Check for new assignments on this task
If oTask.Resources.Count > 0 Then
' Loop through each resource assigned to the task
For Each oResource In oTask.Resources
' Get the email address of the resource
' (You'll need to store email addresses in the project)
' Use the resource's email address to create the email
Set oMail = CreateObject("Outlook.Application").CreateItem(0)
With oMail
.To = oResource.EMailAddress
.Subject = "New Task Assignment"
.Body = "You have been assigned to task: " & oTask.Name
.Send ' Or .Display
End With
Next oResource
End If
Next oTask
End Sub
Rather than loop through tasks, you can loop through resources, and then loop through each resource's assignments. Working at the assignment level, use a flag field to indicate whether or not this assignment has already been emailed.
For example:
Sub EmailNewAssignments()
Dim oOutlook As Object
Set oOutlook = CreateObject("Outlook.Application")
Dim res As Resource
For Each res In ActiveProject.Resources
Dim taskList As String
taskList = vbNullString
Dim asn As Assignment
For Each asn In res.Assignments
If Not asn.Flag1 Then
taskList = taskList & vbCrLf & "Task: " & asn.Task.Name
asn.Flag1 = True
End If
Next asn
If Len(taskList) > 0 Then
Dim oMail As Object
Set oMail = oOutlook.CreateItem(0)
With oMail
.To = res.EMailAddress
.Subject = "New Task Assignments"
.Body = "You have been assigned to the following tasks: " & taskList
.Send
End With
End If
Next res
End Sub
Sub ClearAllAssignmentFlags()
Dim res As Resource
For Each res In ActiveProject.Resources
Dim asn As Assignment
For Each asn In res.Assignments
asn.Flag1 = False
Next asn
Next res
End Sub