1

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.

Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40
gdhc21
  • 107
  • 6
  • 18
  • I can't try right now, but my suspicion is that you run into problems because `objMsg.Attachments.Add (File1)` is different from `objMsg.Attachments.Add File1` (in "completely logical" ways, see [this great answer](https://stackoverflow.com/a/15519085/2822719) for a detailed explanation). So you can try either removing the parenthesis or calling the method like this: `Call objMsg.Attachments.Add(File1)`. – Marcus Mangelsdorf Feb 18 '22 at 16:20

1 Answers1

0

This is not standard.

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

A standard implementation:

Option Explicit

Private Sub Application_Reminder(ByVal Item As Object)

Dim File1 As String
Dim File2 As String

Dim objMsg As MailItem

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

Set objMsg = 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.Display  'objMsg.Send

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52