• 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

Email Worksheets as PDF|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Email Worksheets as PDF|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 & MacrosEmail Worksheets as PDF
sp_PrintTopic sp_TopicIcon
Email Worksheets as PDF
Avatar
Dejan T

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
October 17, 2019
sp_UserOfflineSmall Offline
1
October 17, 2019 - 10:16 pm
sp_Permalink sp_Print

Hi all, first post also and complete novice in VBA. I have read all the comments in the post regarding this vba Code and tried to compile the Code for my needs.

The idea is to create and sent email with pdf attached for each worksheet to specific individual emails.

Either i receive error or the Code is sending the first sheet in Loop all the time:

I am using the following code but am not able to get it to work. Could you please help me to resolve the issue?

 

I appreciate any assistance you can provide

Code:

Sub SendAllSheets()
 Dim ws As Worksheet
 For Each ws In ThisWorkbook.Worksheets
 ws.Activate
If ActiveSheet.Cells(2, "J").Value Like "*@*" Then create_and_email_pdf
 Next
 End Sub
 Sub create_and_email_pdf()
 ' Author - Philip Treacy :: http://www.linkedin.com/in/philiptreacy
 ' http://www.MyOnlineTrainingHub.....th-outlook
 ' Date - 14 Oct 2013
 ' Create a PDF from the current sheet and email it as an attachment through Outlook

Dim EmailSubject 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, Email_Body As String, Email_Body2 As String
 Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
 Dim OverwritePDF As VbMsgBoxResult
 Dim OutlookApp As Object, OutlookMail As Object
 Dim Wks As Worksheet
 Dim ExcludeList As String
 
DestFolder = ThisWorkbook.Path & "\"
 
 CurrentMonth = ActiveSheet.Range("J8")

EmailSubject = ActiveSheet.Name & "_Iveco SP report for "
 OpenPDFAfterCreating = False
 AlwaysOverwritePDF = True
 DisplayEmail = True
 Email_To = ActiveSheet.Range("J2")
 Email_CC = ActiveSheet.Range("J4")
 Email_BCC = ""
 Email_Body = ""

'Create new PDF file name including path and file extension

 PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Name _
 & "-" & CurrentMonth & ".pdf"
 Debug.Print PDFFile

'If the PDF already exists
 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 you want to overwrite the file then delete the current one
 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.CreateItemFromTemplate("C:\Users\F43502C\Desktop\TEST\Austria SP.oft")

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

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

If DisplayEmail = False Then

.Send
End If

End With

Next Wks

End Sub

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
October 21, 2019 - 1:38 pm
sp_Permalink sp_Print

Hi,

There is a "Next Wks" at the end of your code and I was not able to locate the start of this loop, this should be removed, there is no way the code can work as you posted it.

Are there other codes in your file? Maybe in sheet modules?

If you can upload a sample file, will be much better.

Avatar
Dejan T

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
October 17, 2019
sp_UserOfflineSmall Offline
3
October 21, 2019 - 10:34 pm
sp_Permalink sp_Print

Catalin Bombea said
Hi,

There is a "Next Wks" at the end of your code and I was not able to locate the start of this loop, this should be removed, there is no way the code can work as you posted it.

Are there other codes in your file? Maybe in sheet modules?

If you can upload a sample file, will be much better.  

Hi,

I have removed 'Next Wks' and managed to identify and correct the issues and now i have a working Code with all the Details that i Need. Thanks again for This Code, it is time saving regarding my daily activities.

I would like to ask also, how and where to Change in the Code in order to create the pdf from all worhsheets and attach them in single mail? can this be done?

Regards,

Dejan

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
October 22, 2019 - 12:32 am
sp_Permalink sp_Print sp_EditHistory

I think you should only change this part:

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

to:

ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating

 

But don't use the SendAllSheets code anymore if you make this change, it will no longer work as expected, use only the Sub: create_and_email_pdf()

Avatar
Dejan T

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
October 17, 2019
sp_UserOfflineSmall Offline
5
October 22, 2019 - 5:25 pm
sp_Permalink sp_Print

Catalin Bombea said
I think you should only change this part:

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

to:

ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating

 

But don't use the SendAllSheets code anymore if you make this change, it will no longer work as expected, use only the Sub: create_and_email_pdf()  

Thanks again for the help, your time and support!

It is working flawless now!

Do i have also Option, instead Code to create one pdf with all the worksheets inside, to create separate pdf for each worksheet but to attach them in one mail?

In my case, if I have 10 worksheets, to be able to create 10 pdf's but to attach them to the single mail.

 

Regards,

Dejan

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Amin Khajeh
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
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Lawrence Miu
Jo Saies
Paul Pritchard
Monique Roussouw
Keith Aul
Richard Dunks
Manjula Mangalmurti
Thomas Quidort
Jamie Preece
Bob Smith
Forum Stats:
Groups: 3
Forums: 24
Topics: 6192
Posts: 27146

 

Member Stats:
Guest Posters: 49
Members: 31848
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.