I wold like to be able to send an email with an attachment from Excel using vba and the default email client. So far I have this code that prints an excel sheet and uses its data to create the filename and save it as pdf:
Sub ToPDF()
Dim filesave As FileDialog
Dim rng As Range
Dim PDFfileName As String
Dim formatIndex As Long, i As Long
Set filesave = Application.FileDialog(msoFileDialogSaveAs)
Select Case Sheets("Factura").Range("M1").Value
Case 1: t = 1: f = 1 'page 1
Case Else: f = 1: t = 2 'pages 1-2
End Select
With ThisWorkbook.Worksheets("Factura")
Set rng = .Range("A1:J97")
Data = Format(Range("A13"), "YYYYMMDD")
PDFfileName = .Range("H1").Value & " " & Range("A17").Value & " " & Data
End With
With filesave
.Title = "Save as PDF"
.InitialFileName = PDFfileName
formatIndex = 0
For i = 1 To .Filters.Count
If InStr(1, .Filters(i).Extensions, "pdf", vbTextCompare) > 0 Then formatIndex = i
Next
If formatIndex > 0 Then .FilterIndex = formatIndex
If .Show Then
rng.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=.SelectedItems(1), _
OpenAfterPublish:=False, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
from:=f, _
to:=t, _
Quality:=xlQualityStandard
End If
End With
End Sub
I would like to attach this file to an email (using Thunderbird) with a given text for the subject and the body. Can anyone help?
New contributor
Dani Valverde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.