Create pdf from spreadsheet and email All using code

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
We use Primopdf to convert a spreadsheet to pdf.
I was wondering if there is a way to write code that will:
1) Current print area go to print to Primopdf
2) enter the name for the file from Cell C6
3) Send this file to Outlook
4) Enter the email address from cell M5

Thank You,
Michael
 
Hi Michael, something like this should work:

Code:
Sub CreateEmail()

Dim myTo As String
Dim myCC As String
Dim myDate As String
Dim mySub As String
Dim appOutlook As Object
Dim MailItem As Object
Dim myAttach As String

Set appOutlook = CreateObject("Outlook.Application")
Set MailItem = appOutlook.CreateItem(olMailItem)

myTo = "me@here.com"
myCC = "you@there.com"
myDate = Date
mySub = "Here is the test file I'm sending on " & myDate

myAttach = "C:\MyTestFile.xls" ' Change the directory to your own along with the file name.

With MailItem
    .To = myTo
    .CC = myCC
    .Subject = mySub
    .Body = "This is a simple body."
    .Attachments.Add myAttach
    .Display
End With
    
    Set appOutlook = Nothing
    Set MailItem = Nothing
    
End Sub

You can make the email as simple as you'd like, as in the above example, or as complicated...such as using HTML...

Hope this helps!

Dave (y)
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi Michael, it should, you would just need to change the directory and file name....

Dave
 
Upvote 0
Some how I have to save the new name of the file I created, then add the extension pdf to it???

Michael
 
Upvote 0
Yes, you could use your spreadsheet to tell it where your file is and what the name of it is the same way you use it to tell your code where to save it and with what name. An example is below:

Code:
Sub SaveAs()

Dim myFile As String
Dim myDir As String

myDir = Worksheets("Main").Range("D7").Value
myFile = Worksheets("Main").Range("D8").Value

ThisWorkbook.SaveAs myDir & myFile
    
End Sub

Sub CreateEmail()

Dim myTo As String
Dim myCC As String
Dim myDate As String
Dim mySub As String
Dim appOutlook As Object
Dim MailItem As Object
Dim myAttach As String

Set appOutlook = CreateObject("Outlook.Application")
Set MailItem = appOutlook.CreateItem(olMailItem)

myTo = "me@here.com"
myCC = "you@there.com"
myDate = Date
mySub = "Here is the test file I'm sending on " & myDate
myDir = Worksheets("Main").Range("D7").Value
myFile = Worksheets("Main").Range("D8").Value

myAttach = myDir & myFile ' Change the directory to your own along with the file name.

With MailItem
    .To = myTo
    .CC = myCC
    .Subject = mySub
    .Body = "This is a simple body."
    .Attachments.Add myAttach
    .Display
End With
    
    Set appOutlook = Nothing
    Set MailItem = Nothing
    
End Sub

I've also created a working version of this in an Excel Workbook. If you'd like me to email it to you, just email me at:

dmorri18@"removethispart"ford.com

Dave
 
Upvote 0
Here is the part I have now that creates a pdf. :eek: :eek:
Code:
ActiveWorkbook.Save 
    Path = "C:\Emailed Proposals\" 
        On Error Resume Next 
        ActiveWorkbook.SaveAs Filename:= _ 
            Path & "Proposal" & _ 
        Str(Application.Range("O3").Value), FileFormat:=xlNormal _ 
            , Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _ 
            CreateBackup:=False 
        On Error GoTo 0 
    Application.ActivePrinter = "PrimoPDF on Ne00:" 
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _ 
        "PrimoPDF on Ne00:", Collate:=True 
    Application.ActivePrinter = "EPSON Stylus C86 Series on Ne02:"
:confused:
I need to somehow take this newly named file and enter it in an email. I understand yours that you gave me. But it's for an excel file (Isn't it?) If I could convert the file to pdf. then send it in an email, this would be great.
:)
Thank You,
Michael
 
Upvote 0
Michael

Davers code would attach any file to an email, not just an Excel file.

Just specify the correct path and filename here.
Code:
myAttach = myDir & myFile ' Change the directory to your own along with the file name.
 
Upvote 0
OK,
I won't know what that name will be though will I?
Can I do (as in his example)
myFile = Worksheets("Main").Range("D8").Value &".pdf" or will this put an .xls extention automatically?

I guess I need to know how to combine the 2 pieces of code???

Michael
 
Upvote 0
Michael

Why would that put an Excel extension on the filename?
 
Upvote 0
Hey again! You would just need to replace this line in the email code:

Code:
myAttach = myDir & myFile ' Change the directory to your own along with the file name.

with this line that you created:

Code:
myFile = Worksheets("Main").Range("D8").Value &".pdf"

and then change the attachment line of code:

Code:
.Attachments.Add myAttach

to use your name of myFile:

Code:
.Attachments.Add myFile

This way, it will attach the pdf file you created and look for it where you saved it...

Keep posting if you need more help!!

And hello Norie! It's been a while since I've visited the board!!! :) You've helped me out a lot in the past!

Dave
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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