VBA - Attach PDF to new email

telekasi

New Member
Joined
Jan 12, 2018
Messages
1
Hi all!

I'm looking to use VBA to save a spreadsheet as a PDF and then automatically attach the PDF to a new Outlook email. The code below works fine for this, however only if the file is saved in the default location when the 'Save As' window pops up. If the user changes the folder into which the folder is saved, the code can no longer find the file and fails to open a new email.

If anyone knows a solution to this - i.e. a code that finds the file that was just saved under the specific name and attaches this to the email, that would be massively appreciated!

'=========================================
'CREATE AND SAVE PDF
'=========================================

Dim wb As Workbook
Dim ws As Worksheet
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

Set wb = ActiveWorkbook
Set ws = TermSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved

strPath = wb.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & ""

'name PDF file (strFile) - "Client, Issue, Date_Time"
strName = Range("B4")
strFile = strName & ", " & strTime & ".pdf"
strPathFile = strPath & strFile

'enter name and select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If

'=========================================
'OPEN OUTLOOK EMAIL AND ATTACH PDF
'=========================================

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

'Open new Outlook email
With OutMail
.To = Range("C12") & "; " & Range("C13") & "; " & Range("C14") & "; " _
& Range("C15") & "; " & Range("C16")
.Subject = "TEST EMAIL" & " " & Range("B4")
.Body = "Test email - please ignore"
.Attachments.Add strPathFile
.Display
End With
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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