I've got a weekly timesheets spreadsheet that I use for my payrun each week and I was hoping to use a macro to print the individual sheets for each staff member to a pdf and then email it to them.
I've got the intended filename for the pdf in cell B1 on each sheet
The staff members email address is in cell E1
The staff members name is in cell C2
I've written the following macro combining various bits of code from other posts, but I'm pretty sure there are some glitches and it could be improved.
If I'm not on one of the timesheet pages, then when I try to run the macro, I get the following error
"Run-time error '-2147024894 (80070002)':
Cannot find this file. Verify the path and file name are correct.
and when I go into the debugger, it highlights line 40
.Attachments.Add ActiveWorkbook.Path & "" & ActiveSheet.Range("B1").Value & ".pdf"
And if I run the macro when I'm on one of the sheets, it send multiple emails to some team members which is a bit annoying.
If the gurus on here have any advice, that would be greatly appreciated. Thanks!
I've got the intended filename for the pdf in cell B1 on each sheet
The staff members email address is in cell E1
The staff members name is in cell C2
I've written the following macro combining various bits of code from other posts, but I'm pretty sure there are some glitches and it could be improved.
Code:
Sub PrintPDF_ThenEmail()Dim ws As Worksheet
Application.DisplayAlerts = True
For Each ws In Worksheets
If ws.Name <> "Control" And ws.Name <> "Inputs" And ws.Name <> "BarberTemplate" And ws.Name <> "RcptnTemplate" And ws.Name <> "Summary" And ws.Name <> "PayRun" And ws.Name <> "TargetActual" Then
ws.Select
' Create PDF file
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ActiveWorkbook.Path & "\" & ActiveSheet.Range("B1").Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
' Use already open Outlook if possible
Dim OutlApp As Object
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
' Prepare e-mail with PDF attachment
With OutlApp.CreateItem(0)
' Prepare e-mail
.Subject = "Timesheet " & ActiveSheet.Range("C5").Value
.To = ActiveSheet.Range("E1").Value
.Body = "Hi " & ActiveSheet.Range("C2").Value & "," & vbLf & vbLf _
& "Please find attached your timesheet for the week ending " & ActiveSheet.Range("C5").Value & vbLf & vbLf _
& "Kind regards," & vbLf _
& Application.UserName & vbLf & vbLf
.Attachments.Add ActiveWorkbook.Path & "\" & ActiveSheet.Range("B1").Value & ".pdf"
' Try to send
.Send
Application.Visible = True
End With
Next ws
Application.DisplayAlerts = True
End Sub
If I'm not on one of the timesheet pages, then when I try to run the macro, I get the following error
"Run-time error '-2147024894 (80070002)':
Cannot find this file. Verify the path and file name are correct.
and when I go into the debugger, it highlights line 40
.Attachments.Add ActiveWorkbook.Path & "" & ActiveSheet.Range("B1").Value & ".pdf"
And if I run the macro when I'm on one of the sheets, it send multiple emails to some team members which is a bit annoying.
If the gurus on here have any advice, that would be greatly appreciated. Thanks!