Skip to content

Instantly share code, notes, and snippets.

@stevehenderson
Last active December 17, 2015 08:39
Show Gist options
  • Save stevehenderson/5581472 to your computer and use it in GitHub Desktop.
Save stevehenderson/5581472 to your computer and use it in GitHub Desktop.
VBA Code to attach a spreadsheet to an outlook email
Sub SendResultsFull()
Dim OutApp As Object
Dim OutMail As Object
Dim currentPath As String
currentPath = Application.ActiveWorkbook.Path
currentPath = "\\se\ABETWorking\SEN0_CapstoneDA"
Dim csvPath As String
Dim emailBody As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
csvPath = currentPath & "\results_" & Worksheets("MAIN").Range("A2") & ".csv"
emailBody = writeCSV(csvPath)
If emailBody <> "Error" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olmailitem)
'.SaveAs TempFilePath & TempFileName & FileExtStr, _
' FileFormat:=FileFormatNum
'On Error GoTo ErrHandler
' Change the mail address and subject in the macro before
' running the procedure.
With OutMail
.To = "foo@bar.edu"
.CC = ""
.BCC = ""
.Subject = "SE Direct Assessment from " & Worksheets("MAIN").Range("A2")
.Body = emailBody
'.Attachments.Add Destwb.FullName
' You can add other files by uncommenting the following statement.
.Attachments.Add (csvPath)
' In place of the following statement, you can use ".Display" to
' display the mail.
.Send
End With
' Delete the file after sending.
'Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
MsgBox ("Thank you for your assessment. It was sent via email to the SE Program Director. Please save this file for your records.")
End If
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Exit Sub
ErrHandler:
For Index = 1 To 500
Debug.Print Error$(Index)
Next Index
' error handling code
Resume Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment