Hi there,
Try the following code and see if it does what you need:
Public Function SendEmail()
Dim objOutlook As Object
Dim objEmail As Object
Dim vDataRange As Variant
Dim sTemporaryFile As String
Dim sCopiesTo As String
Dim sMailText As String
Dim sSubject As String
Dim sSendTo As String
On Error GoTo ErrorEncountered
' Enter a valid location for the temporary file and then save a copy of this file to it
sTemporaryFile = "N:\My Documents\Joe Various\Temporary File.xls"
ThisWorkbook.SaveCopyAs Filename:=sTemporaryFile
' Enter a valid email location
sSendTo = "joe.bloggs.@somewhere.com"
sSubject = "Mail"
sMailText = "Please find report attached" & vbCrLf & "Regards" & vbCrLf & "Joe"
' Check whether or not Outlook is already running, and if not, open it
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
On Error GoTo ErrorEncountered
If objOutlook Is Nothing Then
Set objOutlook = CreateObject("Outlook.Application")
End If
' Create email
Set objEmail = objOutlook.Createitem(0)
With objEmail
.Subject = sSubject
.to = sSendTo
.cc = sCopiesTo
.body = sMailText
.attachments.Add (sTemporaryFile)
.display
.send
End With
' Delete the temporary file (it's already been sent as an email attachment)
Kill sTemporaryFile
' Clear all data fields (Note - using merged cells makes this more complicated!)
For Each vDataRange In Array("D3:E3", "I3:J3", "N3", "E8:E19", "L8:N21")
ActiveSheet.Range(vDataRange).ClearContents
Next vDataRange
MsgBox "Your email has been sent", vbInformation
Set vDataRange = Nothing
Set objOutlook = Nothing
Set objEmail = Nothing
Exit Function
ErrorEncountered:
MsgBox "An error was encountered", vbCritical
End Function
Hope this helps - please let me know how you get on.
Regards,
Greg M
Bookmarks