VBA - Customizing PDF file name and attaching to email

AmeliaBedelia

New Member
Joined
Apr 8, 2018
Messages
19
I am wanting to see if there is a way to change the name of the attached PDF. In the code below it chooses the FileName = wB.FullName. I want to customize the name of the file to be [ Range("LearnerLFName").Value + "-" + Range("CourseName").Value + "-Weekly Report" ]. I have tried:

FileName = Range("LearnerLFName").Value + "-" + Range("CourseName").Value + "-Weekly Report"

And can see it saving something with this name, however when the email displays there is no attachment. The Ranges are referring to named cells in my workbook.

I am hoping someone has a resolution for this. Here is the complete VBA code:


Sub SendEmailPDF()

Dim wB As Workbook
Dim FileName As String
Dim myFile As Variant
Dim OutlookApp As Object
Dim OutlookMail As Object
On Error Resume Next

Set wB = Application.ActiveWorkbook
FileName = wB.FullName
xIndex = VBA.InStrRev(FileName, ".")
If xIndex > 1 Then FileName = VBA.Left(FileName, xIndex - 1)
FileName = FileName & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
With OutlookMail
.To = ""
.CC = ""
.BCC = ""
.Subject = Range("LearnerLFName").Value + " " + Range("CourseName").Value + " Weekly Report"
.body = ""
.Attachments.Add FileName
.Display
End With
Kill FileName
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub


Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top