Excel Macro to print sheets as pdf and then email - bug fix

Status
Not open for further replies.

MykeD

New Member
Joined
Feb 3, 2018
Messages
8
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.
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!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Status
Not open for further replies.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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