I want to create a Macro in MS Project that will send an outlook email when a resource is assigned. My issue is that I want it to only notify the resources that have been assigned since the last time the macro was run because i don't want everyone getting duplicate emails every time.
Currently my code successfully emails every assignee the ask they are assigned to every time its ran. Getting it to only select newly assigned resources since the last run is where I'm stuck and don't really know where to start. Any help is appreciated!!
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 oMail As Object
Set oMail = CreateObject("Outlook.Application").CreateItem(0)
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 = vbCrLf & "Task: " & asn.Task.Name
asn.Flag1 = True
End If
Next asn
If Len(taskList) > 0 Then
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