PDF to Email

jcbaggett16

New Member
Joined
Feb 14, 2024
Messages
7
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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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