PDF to Email

jcbaggett16

New Member
Joined
Feb 14, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
All,

I am trying to save a file to PDF then pull that file and attach to email. I can get it to save to a file path but I can not get it to pull from the file path.

Sub PDF()
ChDir "C:\Users\...\Desktop\New folder"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\...\Desktop\New folder\Transition " & Range("L2").Text


Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAttachments As Object

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set myAttachments = OutLookMailItem.Attachments

With OutLookMailItem
.To = ""
.Subject = ""
.Body = ""
myAttachments.Add "C:\Users\...\Desktop\New folder\Test.PDF"
'.send
.Display
End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing


End Sub
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try adding
Dim Strfilename as String
Strfilename = "C:\Users\...\Desktop\New folder\Test.PDF" 'above the with block

Then changing myattachements.add to
.Attachments.add Strfilename
 
Upvote 0
Try adding
Dim Strfilename as String
Strfilename = "C:\Users\...\Desktop\New folder\Test.PDF" 'above the with block

Then changing myattachements.add to
.Attachments.add Strfilename

This is what I have Now,

Sub PDF()
ChDir "C:\Users\Desktop\New folder"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\Desktop\New folder\Transition " & Range("L2").Text


Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim Attachments As Object
Dim Strfilename As String
Strfilename = "C:\Users\Desktop\New folder\Transition " & Range("L2").PDF

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set Attachments = OutLookMailItem.Attachments

With OutLookMailItem
.To = ""
.Subject = ""
.Body = ""
Attachments.Add Strfilename
'.send
.Display
End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing


End Sub
 
Upvote 0
.Attachments.Add

You need the full stop first.
Also when testing this, can you confirm that the file exists in the location you provided.
When running the macro, confirm that Range("L2") has a value at the time of the Strfilename
Do this in the immediate window as add a debug.print Range("L2").Value just before Strfilename = "C:\Users\Desktop\New folder\Transition " & Range("L2").PDF
 
Upvote 0
.Attachments.Add

You need the full stop first.
Also when testing this, can you confirm that the file exists in the location you provided.
When running the macro, confirm that Range("L2") has a value at the time of the Strfilename
Do this in the immediate window as add a debug.print Range("L2").Value just before Strfilename = "C:\Users\Desktop\New folder\Transition " & Range("L2").
 
Upvote 0
Got it to work, now it only attaches to outlook only if outlook is already open. How would I get it to open outlook if its not already open?

Sub PDF()
ChDir "C:\Users\...\Desktop\New folder"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\...\Desktop\New folder\Transition " & Range("H2").Text


Dim OutLookApp As Object
Dim OutLookMailItem As Object
Dim myAttachments As Object

Set OutLookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutLookApp.CreateItem(0)
Set myAttachments = OutLookMailItem.Attachments

With OutLookMailItem
.To = ""
.Subject = ""
.Body = ""
myAttachments.Add "C:\Users\...\Desktop\New folder\Transition " & Range("H2").Text & ".pdf"
'.send
.Display
End With

Set OutLookMailItem = Nothing
Set OutLookApp = Nothing


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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