Emailing Muliple sheets macro

tkellymd

New Member
Joined
May 29, 2017
Messages
12
I have a workbook with multiple worksheet that I need to be able each sheet to a different address. I found the following VBA macro that will convert the active sheet to a pdf, append it to an email, insert the email address, and save the pdf into a file. I then have to manually send the email. I need the macro to do this for all the appropriate worksheets (I have a few worksheets that are for common variable and do not need to be emailed) and email and save them automatically at the same time when I run the macro. I found a loop subroutine that is below the first macro but am not sure how to integrate it into the rest of the program. I know little about VBA and Macros so appreciate any help you can provide.

Option Explicit

Sub create_and_email_pdf()
' Author - Philip Treacy :: https://www.linkedin.com/in/philiptreacy
' https://www.MyOnlineTrainingHub.com...om-excel-worksheet-then-email-it-with-outlook
' Date - 14 Oct 2013
' Create a PDF from the current sheet and email it as an attachment through Outlook

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 = ""

' *****************************************************
' ***** You Can Change These Variables *********

EmailSubject = "Performance Improvement Bonus Calculation " 'Change this to change the subject of the email. The current month is added to end of subj line
OpenPDFAfterCreating = False 'Change this if you want to open the PDF after creating it : TRUE or FALSE
AlwaysOverwritePDF = False 'Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE
DisplayEmail = True 'Change this if you don't want to display the email before sending. Note, you must have a TO email address specified for this to work
Email_To = ActiveSheet.Range("E1") 'Change this if you want to specify To email e.g. ActiveSheet.Range("H1") to get email from cell H1
Email_CC = ""
Email_BCC = ""

' ******************************************************

'Prompt for file destination
With Application.FileDialog(msoFileDialogFolderPicker)

If .Show = True Then

DestFolder = .SelectedItems(1)

Else

MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"

Exit Sub

End If

End With

'Current month/year stored in M1 (this is a merged cell)
CurrentMonth = Mid(ActiveSheet.Range("M1").Value, InStr(1, ActiveSheet.Range("M1").Value, " ") + 1)

'Create new PDF file name including path and file extension
PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Name _
& "_" & CurrentMonth & ".pdf"

'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.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



Here is the Loop subroutine I found if this helps:

For Each Wks in ThisWorkbook.Worksheets
PDFFile = DestFolder & Application.PathSeparator & Wks.Name & “.pdf”
'...create email code
'write the destination address:
.To=Wks.Cells(2,"M")
'attach the file
.Attachments.Add PDFFile
'...rest of code
Next


and I also found this.

Dim Wks as Worksheet
For Each Wks in Thisworkbook.Worksheets
Application.Goto wks.cells(1,1)
'you are now in that sheet, do what you need, use Wks.Name if you need the name of current sheet in your code...
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This is certainly closer to what I need. Seems like I need to use parts of this in combination with the macro I already have so I get the other functionality. I see that this script will attach and email each spreadsheet to a separate email address. What about appending the date to the file name and email subject and also saving the file as a pdfs to the appropriate folder? Thanks for your help.
 
Upvote 0
Here is what I have so far but it is telling me I have not defined the CurrentYear variable which I have done....I think.

Option Explicit
Sub create_and_email_pdf()
Dim sh As Worksheet
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
CurrentYear = ""

' *****************************************************
' ***** You Can Change These Variables *********

EmailSubject = "Performance Improvement Bonus Calculation " 'Change this to change the subject of the email. The current month is added to end of subj line
OpenPDFAfterCreating = False 'Change this if you want to open the PDF after creating it : TRUE or FALSE
AlwaysOverwritePDF = False 'Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE
DisplayEmail = True 'Change this if you don't want to display the email before sending. Note, you must have a TO email address specified for this to work
Email_To = sh.Range("E1").Value 'Change this if you want to specify To email e.g. ActiveSheet.Range("H1") to get email from cell H1
Email_CC = ""
Email_BCC = ""

' ******************************************************

For Each sh In ThisWorkbook.Worksheets
If sh.Range("E1").Value Like "?*@?*.?*" Then

'Set file destination
DestFolder = C:\Users\tkell\OneDrive\GHA Critical Care\Administrative\Human Resources\Bonuses\Bonus Calculations

'Current year stored in M1
CurrentYear = Mid(sh.Range("M1").Value, InStr(1, sh.Range("M1").Value, " ") + 1)

'Create new PDF file name including path and file extension
PDFFile = DestFolder & Application.PathSeparator & sh.Name _
& "_" & CurrentYear & ".pdf"

'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
sh.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
.Body -"Attached is your Perfomance Improvement bonus calculation for the current period. If you have any questions I would be happy to discuss them with you."
.Attachments.Add PDFFile

If DisplayEmail = False Then

.Send

End If

End With

End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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