0
votes

Using Outlook, I flag an email if it contains a job for me to do. When I've completed the job I mark the email complete (by changing the flag to a tick). I would like to automatically send a reply telling the initial sender the job has been done.

I found the below code which seems like it would do half of what I want but instead of "John Smith" I want it to use the sender's email address and preferably contain the initial email as it would if I sent a reply.

Public WithEvents olItems As Outlook.Items

Private Sub Application_Startup()
    Set olItems = Session.GetDefaultFolder(olFolderTasks).Items
End Sub

Private Sub olItems_ItemChange(ByVal Item As Object)
    Dim obApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim Recip As String

    'Replace "test" as per your needs
    If InStr(LCase(Item.Subject), "test") > 0 And Item.Complete = True Then
       'Replace with your desired contact
       Recip = "John Smith"
       If MsgBox("Do you want to send a report to " & Recip & " ?", vbYesNo + vbQuestion, "Confirm Sending Report") = vbYes Then
          Set obApp = Outlook.Application
          Set olMail = obApp.CreateItem(olMailItem)
          With olMail
               .To = Recip
               .Subject = "Complete: " & Item.Subject
               .Body = "Dear Mr. Smith" & vbCrLf & "I've completed this task in " & DateDiff("d", Item.CreationTime, Now) & " day" & Chr(40) & "s" & Chr(41) & "." & vbCrLf & vbCrLf & "Task Name: " & Item.Subject & vbCrLf & "Start Date: " & Item.StartDate & vbCrLf & "Due Date: " & Item.DueDate & vbCrLf & "Creation Time: " & Item.CreationTime & vbCrLf & "Completed Time: " & Now & vbCrLf & vbCrLf & "Task Details: " & vbCrLf & Item.Body
               .ReadReceiptRequested = True
               'To directly send it,use ".Send" instead
               .Display
          End With
       End If
    End If
End Sub
1

1 Answers

0
votes

Instead of creating a new mail item in the code:

Set olMail = obApp.CreateItem(olMailItem)

You can just reply to the existing one preserving the message body and recipients inplace:

Set olMail = Item.Reply()

Note, you can also use the MailItem.ReplyAll method which creates a reply to all original recipients from the original message.

Public WithEvents olItems As Outlook.Items

Private Sub Application_Startup()
    Set olItems = Session.GetDefaultFolder(olFolderTasks).Items
End Sub

Private Sub olItems_ItemChange(ByVal Item As Object)
    Dim obApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim Recip As String

    'Replace "test" as per your needs
    If InStr(LCase(Item.Subject), "test") > 0 And Item.Complete = True Then
       'Replace with your desired contact
       Recip = "John Smith"
       If MsgBox("Do you want to send a report to " & Recip & " ?", vbYesNo + vbQuestion, "Confirm Sending Report") = vbYes Then          
          Set olMail = Item.Reply
          With olMail
               .Subject = "Complete: " & Item.Subject
               .Body = "Dear Mr. Smith" & vbCrLf & "I've completed this task in " & DateDiff("d", Item.CreationTime, Now) & " day" & Chr(40) & "s" & Chr(41) & "." & vbCrLf & vbCrLf & "Task Name: " & Item.Subject & vbCrLf & "Start Date: " & Item.StartDate & vbCrLf & "Due Date: " & Item.DueDate & vbCrLf & "Creation Time: " & Item.CreationTime & vbCrLf & "Completed Time: " & Now & vbCrLf & vbCrLf & "Task Details: " & vbCrLf & Item.Body
               .ReadReceiptRequested = True
               'To directly send it,use ".Send" instead
               .Display
          End With
       End If
    End If
End Sub