'Adding attachments to automatic emails

I cobbled together something to send automatic emails when a calendar reminder goes off in Outlook. It was working until I tried to add attachments. It doesn't throw any errors but it also doesn't send the reminder emails.

Private Sub Application_Reminder(ByVal Item As Object)

  Set objMsg = Application.CreateItemFromTemplate("C:\Users\glenndc\AppData\Roaming\Microsoft\Templates\Weekly.oft")

  If Item.MessageClass <> "IPM.Appointment" Then
    Exit Sub
  End If

  If Item.Categories <> "WeeklyTest" Then
    Exit Sub
  End If

  objMsg.To = Item.Location
  objMsg.Subject = Item.Subject
  objMsg.Body = Item.Body

  objMsg.Send

  Set objMsg = Nothing
End Sub

This was working and sending the emails.

Private Sub Application_Reminder(ByVal Item As Object, _
Optional ByVal File1 As String = "", _
Optional ByVal File2 As String = "")

  File1 = "H:\New Patient Intake Packet.pdf"
  File2 = "H:\Scheduling a VATS patient.pdf"

  Set objMsg = Application.CreateItemFromTemplate("C:\Users\glenndc\AppData\Roaming\Microsoft\Templates\Weekly.oft")

  If Item.MessageClass <> "IPM.Appointment" Then
    Exit Sub
  End If

  If Item.Categories <> "WeeklyTest" Then
    Exit Sub
  End If

  objMsg.To = Item.Location
  objMsg.Subject = Item.Subject
  objMsg.Body = Item.Body
  If FileExists(File1) Then
    objMsg.Attachments.Add (File1)
  End If
  If FileExists(File2) Then
    objMsg.Attachments.Add (File2)
  End If

  objMsg.Send

  Set objMsg = Nothing
End Sub

This won't send the email but isn't giving any errors.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source