Skip to content

Instantly share code, notes, and snippets.

@pudelosha
Last active January 27, 2017 15:42
Show Gist options
  • Save pudelosha/a3fe5098f7e2e122dd206b5fc9a9c688 to your computer and use it in GitHub Desktop.
Save pudelosha/a3fe5098f7e2e122dd206b5fc9a9c688 to your computer and use it in GitHub Desktop.
XLS to Outlook
Option Explicit
Private OTapp As Outlook.Application
Private OTemail As Outlook.MailItem
Private strTo As String
Private strSubject As String
Private strAttachmentPath As String
Private strBodyHTML As String
Private Sub Class_Initialize()
On Error Resume Next
Set OTapp = GetObject(, "Outlook.Application")
If OTapp Is Nothing Then
MsgBox "MS Outlook application is not running!"
Exit Sub
End If
On Error GoTo 0
End Sub
Private Sub Class_Terminate()
Set OTapp = Nothing
End Sub
Property Let EmailTo(strEmailTo As String)
strTo = strEmailTo
End Property
Property Let EmailSubject(strEmailSubject As String)
strSubject = strEmailSubject
End Property
Property Let EmailBody(strEmailBody As String)
strBodyHTML = strEmailBody
End Property
Property Let EmailAttachment(strEmailAttachment As String)
strAttachmentPath = strEmailAttachment
End Property
Sub SendEmail()
If strTo = "" Or IsNull(strTo) Then
MsgBox "Receipient name was not provided!"
Exit Sub
End If
If strSubject = "" Or IsNull(strSubject) Then
MsgBox "Email subject was not provided!"
Exit Sub
End If
If strBodyHTML = "" Or IsNull(strBodyHTML) Then
MsgBox "Email body was not provided!"
Exit Sub
End If
Set OTemail = OTapp.CreateItem(0)
With OTemail
.Display
.To = strTo
.Subject = strSubject
.HTMLBody = strBodyHTML
If strAttachmentPath <> "" Then
.Attachments.Add strAttachmentPath
End If
.Send
End With
Set OTemail = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment