Help....Email Sheets To Different People

GregJamesB

New Member
Joined
Dec 5, 2018
Messages
3
Please Help

I am running my payroll through excel and currently printing everything. I am looking to email everything instead. I want to be able to click a button and each employees paystub be emailed to them. Not sure if this is possible with a Macro or what coding to use.

Thanks
Greg
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
this assumes all sheets are to be sent,
and the target email is in cell E2,
change as you need.

Code:
Sub SendAllEmails()
Dim sht As Worksheet
Dim vEmail, vSubj, vBody, vFile
Dim sFile As String
For Each sht In Sheets
  sht.Activate
  vEmail = Range("E2").Value    'get email off sheet
  vFile = "C:\temp\" & sht.Name & ".pdf"
    
    'make the pdf bill
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFile, Quality:=xlQualityStandard, IncludeDocProperties:=True
     'send email
  Email1 vEmail, vSubj, vBody, vFile
Next
MsgBox "Done"
End Sub

Public Function Email1(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
On Error GoTo ErrMail
'NOTE : YOU MUST HAVE THE OUTLOOK REFERENCE CHECKED IN VBE; ctl-G, menu,tools, references, Microsoft Outlook XX Object library
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.createitem(olmailitem)
With oMail
    .To = pvTo
    .Subject = pvSubj
    If Not IsNull(pvBody) Then .Body = pvBody
    If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
    
    '.Display True
    .Send
End With
Email1 = True
Endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function
ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume Endit
End Function
 
Upvote 0
Thank you for your quick response.
I am going to give it a shot.


this assumes all sheets are to be sent,
and the target email is in cell E2,
change as you need.

Code:
Sub SendAllEmails()
Dim sht As Worksheet
Dim vEmail, vSubj, vBody, vFile
Dim sFile As String
For Each sht In Sheets
  sht.Activate
  vEmail = Range("E2").Value    'get email off sheet
  vFile = "C:\temp\" & sht.Name & ".pdf"
    
    'make the pdf bill
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFile, Quality:=xlQualityStandard, IncludeDocProperties:=True
     'send email
  Email1 vEmail, vSubj, vBody, vFile
Next
MsgBox "Done"
End Sub

Public Function Email1(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
On Error GoTo ErrMail
'NOTE : YOU MUST HAVE THE OUTLOOK REFERENCE CHECKED IN VBE; ctl-G, menu,tools, references, Microsoft Outlook XX Object library
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.createitem(olmailitem)
With oMail
    .To = pvTo
    .Subject = pvSubj
    If Not IsNull(pvBody) Then .Body = pvBody
    If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
    
    '.Display True
    .Send
End With
Email1 = True
Endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function
ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume Endit
End Function
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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