Sending Automated Emails Using Outlook Calendar

126 Views Asked by At

In Outlook 2016 I generate automated emails using a recurring calendar appointment.

Private WithEvents olRemind As Outlook.Reminders
Dim strSubject As String

Private Sub Application_Reminder(ByVal Item As Object)
    Set olRemind = Outlook.Reminders

    'IPM.TaskItem to watch for Task Reminders
    If Item.MessageClass <> "IPM.Appointment" Then
        Exit Sub
    End If

    If Item.Categories <> "Send Message" Then
        Exit Sub
    End If

    strSubject = Item.Subject

    Dim objMsg As MailItem
    Set objMsg = Application.CreateItem(olMailItem)

    objMsg.To = Item.Location
    objMsg.BCC = ""
    objMsg.Subject = strSubject
    objMsg.Body = Item.Body
    objMsg.Attachments.Add "C:\Users\ABC\Desktop\XYZ.pdf"
    objMsg.Send
    'objMsg.Display

    Set objMsg = Nothing
End Sub

Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
    For Each objRem In olRemind
        If objRem.Caption = strSubject Then
            If objRem.IsVisible Then
                objRem.Dismiss
                Cancel = True
            End If
            Exit For
        End If
    Next objRem
End Sub

The appointment the macro is keying off of:
enter image description here

I have two issues:

  1. The formatting (highlighting, underlining, text color, etc.) of the calendar item body is not carrying over into the body of the email.
  2. When adding an attachment to my email is there a way to reference the attachment of the calendar item rather than referencing the text file path where that attachment is saved?

I tried HTMLbody but that does not fix issue #1.

1

There are 1 best solutions below

1
Eugene Astafiev On

The formatting (highlighting, underlining, text color, etc.) of the calendar item body is not carrying over into the body of the email.

The Body property is a plain text string. You need to use the HTMLBody property if you want to see any formatted text on the email. However, the AppointmentItem class doesn't provide the HTMLBody property, to read the HTML markup from calendar items you need to read the PR_HTML property value. See Get HTML body from Appointment for more information.

Calendar items use the RTFBody property which has RTF formatting, the Outlook object model doesn't expose the HTMLBody property for calendar items.

When adding an attachment to my email is there a way to reference the attachment of the calendar item rather than referencing the text file path where that attachment is saved?

Nope. You need to save the file to the disk. The Attachments.Add method accepts a string which can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

A low-level API (Extended MAPI) allows dealing with attachments binary data on the fly without any disk operations, but not the Outlook object model. Typically to you need to set the PR_ATTACH_DATA_BIN property which holds the attachment when the value of the PR_ATTACH_METHOD property is ATTACH_BY_VALUE, which is the usual attachment method and the only one required to be supported.