• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

vba to create pdf from excel worksheet then email|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / vba to create pdf from excel worksheet then email|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & Macrosvba to create pdf from excel worksh…
sp_PrintTopic sp_TopicIcon
vba to create pdf from excel worksheet then email
Avatar
Robert Rogozinski

New Member
Members
Level 0
Forum Posts: 1
Member Since:
March 1, 2019
sp_UserOfflineSmall Offline
1
March 1, 2019 - 7:50 am
sp_Permalink sp_Print sp_EditHistory

Hi there! 

I'm working with some sample code posted by Phillip a while back. For the most part it is working but being so new to this I'm having troubles when customizing it to fit our needs more. 

Right now the code send one email of one sheet to the specified recipients but I would like for the script to refresh the workbook then loop through all of the sheets to create the pdf's, attach the 5 pdf's to one email and then send to their final destination. If thats not possible or too much hassle 5 separate emails are fine as long as I can figure out the rest... I found the code I believe I need for this I just cant for the life of me figure out how to write it correctly. Any help would be greatly appreciated. Think im going to take a couple beginner courses online once this project gets wrapped up. 

Oh, I would also like to set up this to be ran with the task scheduler. I found a great tutorial I just literally dont know how to write the code with what I have below. 

Task Scheduler:

https://www.excell-en.com/exce.....g-tutorial

Sub Auto_Open()

Application.DisplayAlerts = False
Application.ScreenUpdating = False

MsgBox "Excellen" 'Replace this with your macro code!


Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
_______________________________________________________________

Loop through all sheets:

Sub SendAllSheets()
Dim ws As Worksheet
For Each ws In Worksheets
   ws.Activate
   create_and_email_pdf
Next
 End Sub

_______________________________________________________________

Refresh Workbook on open

 

'ActiveSheet.EnableCalculation = True

 

_______________________________________________________________

My Code

Sub create_and_email_pdf()
ActiveSheet.EnableCalculation = True

Dim EmailSubject As String, EmailSignature As String
Dim CurrentMonth As String, DestFolder As String, PDFFile As String
Dim Email_To As String, Email_CC As String, Email_BCC As String
Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
Dim OverwritePDF As VbMsgBoxResult
Dim OutlookApp As Object, OutlookMail As Object
CurrentMonth = ""

EmailSubject = ActiveSheet.Range("A1")
OpenPDFAfterCreating = False
AlwaysOverwritePDF = True
DisplayEmail = False
Email_To = "Robert.rogozinski@clearsourcebpo.com"
Email_CC = "Robert.rogozinskislc@gmail.com"
Email_BCC = "zinski89@gmail.com"
DestFolder = "C:\DailyWfmPDF"

CurrentMonth = ActiveSheet.Range("A2")

PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Range("A1") & ".pdf"

If Len(Dir(PDFFile)) > 0 Then

If AlwaysOverwritePDF = False Then

OverwritePDF = MsgBox(PDFFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")

On Error Resume Next

If OverwritePDF = vbYes Then

Kill PDFFile

Else

MsgBox "OK then, if you don't overwrite the existing PDF, I can't continue." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"

Exit Sub

End If

Else

On Error Resume Next
Kill PDFFile

End If

If Err.Number <> 0 Then

MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"

Exit Sub

End If

End If

'Create the PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating

'Create an Outlook object and new mail message
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)

'Display email and specify To, Subject, etc
With OutlookMail

.Display
.To = Email_To
.CC = Email_CC
.BCC = Email_BCC
.Subject = EmailSubject & CurrentMonth
.Attachments.Add PDFFile

If DisplayEmail = False Then

.Send

End If

End With

End Sub

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1517
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
March 6, 2019 - 10:45 am
sp_Permalink sp_Print sp_EditHistory

Hi Robert,

Please see the attached workbook.  By moving the code that loops though each workbook, you can create a PDF for each and then add each of them to the same email.

Please note : The value for EmailSubject is set once at the top off the Sub and takes the value of ActiveSheet.Range("A1").  So you wouldn't want to set this every time you moved to another sheet.

Regarding the scheduling of this, you should use a workbook open event, not Auto_Open and then just set the Windows scheduler to open the workbook.

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Anders Sehlstedt, RAQUEL ACION, Chandler Davis, gogy13, Brenda Richards, Mark Luke
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 202
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Mark Luke
terimeri dooriyan
Jack Aston
AndyC
Denise Lloyd
michael serna
mashal sana
Tiffany Kang
Leah Gillmore
Sopi Yuniarti
Forum Stats:
Groups: 3
Forums: 24
Topics: 6218
Posts: 27267

 

Member Stats:
Guest Posters: 49
Members: 31901
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.