Active Member
October 17, 2019
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
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
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.
Active Member
October 17, 2019
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
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
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()
Active Member
October 17, 2019
Catalin Bombea said
I think you should only change this part:ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreatingto:
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
1 Guest(s)