Sub Attach_ActiveSheet_As_Pdf_With_Signature()
' ZVI:2016-05-31 http://www.mrexcel.com/forum/excel-questions/710212-visual-basic-applications-code-convert-excel-pdf-email-attachment-4.html#post4537766
' --> User settings, change to suit
Const IsHtml As Boolean = False ' Change to True for HTML body of email
Const IsDisplay As Boolean = False ' Change to True to .Display instead of .Send
Const IsSilent As Boolean = False ' Change to True to Send without the confirmation MsgBox
' <-- End of settings
Dim IsCreated As Boolean
Dim MailSubject As String, PdfFile As String, s As String
Dim HtmlSignature As String, Signature As String
Dim OutlApp As Object
Dim i As Long
Dim char As Variant
' Subject of the email, choose one of two below lines
'MailSubject = Range("A1") & " " & Date
MailSubject = "Report on " & Date
' Define PDF filename
PdfFile = ActiveWorkbook.Name
i = InStrRev(PdfFile, ".xl", , vbTextCompare)
If i > Len(PdfFile) - 5 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = PdfFile & "_" & ActiveSheet.Name
' Clean up the name of PDF file
For Each char In Split("? "" / \ < > * | :")
PdfFile = Replace(PdfFile, char, "_")
Next
' Add %TEMP% path to the file name and limit too long name
PdfFile = Left(CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) & "\" & PdfFile, 251) & ".pdf"
'Debug.Print PdfFile
' Try to delete PDF file for the case it was not deleted at debugging
If Len(Dir(PdfFile)) Then Kill PdfFile
' Export activesheet as PDF to the temporary folder
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
' Use the already open Outlook if possible
On Error Resume Next
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
On Error GoTo 0
' Prepare email with PDF attachment and default signature
With OutlApp.CreateItem(0)
' Add the attachment first for correct attachment's name with non English symbols
.Attachments.Add PdfFile
' Get default email signature without blinking (instead of .Display method)
With .GetInspector: End With
If IsHtml Then HtmlSignature = .HTMLBody Else Signature = .Body
' Prepare e-mail (uncommenmt and fill the lines below)
.Subject = MailSubject
'.To = "..." ' <-- Put email(s) of the recipient(s) here
'.CC = "..." ' <-- Put email of 'copy to' recipient(s) here
' Edit the body's text or html text as required
If IsHtml Then
' The tags are: h3 is for Header#3; b is for Bold; br is for line break
' HTML tag's brakets are not displayed properly in the forum post, thus replacing in s is used to fix this problem
s = "(h3)(b)Dear Customer,(/b)(/h3)" _
& "This e-mail was created by the code of this post - " _
& "(a HREF=""http://www.mrexcel.com/forum/excel-questions/710212-visual-basic-applications-code-convert-excel-pdf-email-attachment-4.html#post4537766"")Attach_ActiveSheet_As_Pdf_With_Signature(/a)" _
& "(br /)" _
& "(b)The report is attached in PDF file(/b)"
s = Replace(s, "(", "<")
s = Replace(s, ")", ">")
.HTMLBody = s & HtmlSignature
Else
.Body = "Dear Customer," _
& vbLf & vbLf _
& "This e-mail was created by the code of this post:" _
& vbLf _
& "http://www.mrexcel.com/forum/excel-questions/710212-visual-basic-applications-code-convert-excel-pdf-email-attachment-4.html#post4537766" _
& vbLf & vbLf _
& "The report is attached in PDF file" _
& Signature
End If
' Try to send or just display the e-mail
On Error Resume Next
If IsDisplay Then .Display Else .Send
' Show error of .Send method
If Not IsDisplay Then
' Return focus to Excel's window
Application.Visible = True
' Report on error or success
If Err Then
MsgBox "E-mail was not sent for some reasons" & vbLf & "Please check it", vbExclamation
.Display
Else
If Not IsSilent Then
MsgBox "E-mail successfully sent", vbInformation
End If
End If
End If
On Error GoTo 0
End With
' Delete the temporary PDF file
If Len(Dir(PdfFile)) Then Kill PdfFile
' Try to quit Outlook if it was not previously open
If IsCreated Then OutlApp.Quit
' Try to release the memory of object variable
Set OutlApp = Nothing
End Sub