Hi,
Please can someone tell me when the following code will not attach a pdf of my workbook to an email:
Private Sub ExpAndSendPdf()
Dim sFileName As String
Dim sTo As String
Dim sSubject As String
Dim sMeas As String
Dim OutApp As Object
Dim OutMail As Object
Dim Send As Boolean
'Create file name
sFileName = Range("client").Value & " - Managment accounts - " & _
Format(Range("manp").Value, "dd mmm yyyy" & ".pdf") 'Without .pdf
'Check if file exists
If Dir(sFileName & ".pdf") <> "" Then
sFileName = sFileName & Format(Now(), "_hh-mm" & ".pdf")
End If
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=sFileName & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'Send pdf
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'create reciepient string
sTo = ""
'Mail subject
sSubject = "For review - Management accounts for " & Sheets("input sheet").Range("client")
'create measage body
sMeas = "Hello." & vbNewLine & vbNewLine & _
"Please find attached management accounts for review." & vbNewLine & vbNewLine & _
"Thank you."
On Error Resume Next
With OutMail
.To = sTo
.CC = ""
.BCC = ""
.Subject = sSubject
.Body = sMeas
.Attachments.Add (sFileName & ".pdf")
'Change Send = False if you don't want it to send.
Send = False
If Send = True Then
.Send
Else
.Display
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
It appears to do everything correct apart from attach the file.
Also - is it possible to have the pdf output to a temporary file location on the users computer and then clear down when the email has been sent? At the minute this is generating to the location the workbook is saved.
thanks in advance.
Bookmarks