Hi, I'm trying to create a payroll sheet with button to create a password protected pdf and at the same time send it as an email. So I have the below code. My problem is I'm getting this runtime error '-2147024894(80070002)': cannot find this file. I know about this error and where it's coming from. What I don't understand is after I run the macros it creates #N/A and replaces the employee id with the name of employee. I don't know if I'm just sleepy that's why I couldn't find the error. Would really appreciate your help.
grrrr.PNG
Sub CreatePDFprotect()
ActiveWorkbook.Sheets("Masterdata").Activate
totemp = Range("A5").End(xlDown).Row - 4
ActiveWorkbook.Sheets("Payslip").Activate
' If Directory does not exist, Create Directory under Payslips in the name of Month & Year
Dim fso As Object
Dim fldrpath As String
Set fso = CreateObject("scripting.filesystemobject")
fldrpath = Range("N18")
If Not fso.folderexists(fldrpath) Then
fso.createfolder (fldrpath)
End If
If MsgBox("Are you sure you want to mail Salary Slips to " & totemp & " Employees?", vbYesNo, "Confirm") = vbYes Then
Dim cell As Range
Dim MyTimer As Double
Dim x As Integer
Dim statusmsg As Variant
Dim tgap As Integer, pctCompl As Single
Dim fTemp As String
Dim oPdf As String
Dim Pwd As String
With Sheets("Masterdata")
For Each cell In .Range("B5", .Range("B5").End(xlDown))
Sheets("Payslip").Range("B6").Value = cell.Value
' run the mailer code(generate email) here
ActiveWorkbook.Sheets("Payslip").Activate
x = x + 1
MyTimer = Timer
Do
Loop While Timer - MyTimer < 0.03
Application.StatusBar = "Progress: " & x & " of " & totemp & " Employees: Completed: " & Format(x / totemp, "0%")
DoEvents
Range("A1:J43").Select
Pwd = Range("M13").Value
oPdf = Range("N17").Value & ".pdf"
fTemp = Range("N18").Value & "\" & "Temp.Pdf"
ActiveSheet.PageSetup.PrintArea = "$A$1:$J$43"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=fTemp, _
Quality:=xlQualityStandard
fTemp = """" & fTemp & """" 'Putting extra "" around for command Parameter.
oPdf = """" & oPdf & """"
Pwd = """" & Pwd & """"
'Making Command String for making protected PDFs Using PDFtk tool.
cmdStr = "pdftk " & fTemp & " Output " & oPdf & " User_pw " & Pwd & " Allow AllFeatures"
Shell cmdStr, vbHide 'Executing PDFtk Command.
Application.Wait DateAdd("s", 2, Now) 'Allowing 2 secs for command to execute.
' Kill Replace(fTemp, """", "")
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngdear As Range
Dim rngBody As Range
Dim rngAttach As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With ActiveSheet
Set rngTo = .Range("M12")
Set rngSubject = .Range("N20")
Set rngdear = .Range("N19")
Set rngBody = .Range("N21")
Set rngAttach = .Range("N22")
End With
SigString = "C:/Users/DianeTana/AppData/Roaming/Microsoft/Signatures/Internal.htm"
' If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
' Else
' Signature = ""
' End If
With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.HTMLBody = "<br>" & rngdear.Value & "<br><br>" & rngBody.Value & "<br><br>" & Signature
.Attachments.Add rngAttach.Value
.Save 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing
tgap = tgap + 1
' Pause 1 minute
If tgap = 200 Then
Application.StatusBar = "Paused for Time Delay..... Please wait...."
Application.Wait (Now + #12:01:00 AM#)
tgap = 1
End If
pctCompl = (x / totemp) * 100
progress pctCompl
Next cell
End With
Dim OutPut As Integer
OutPut = MsgBox("Succesessfully Completed the Task.", vbInformation, "Payslip Generation")
Application.StatusBar = False
End If
End Sub
Sub CreatePDFone()
If MsgBox("Are you sure you want to mail Salary Slip to this Employee?", vbYesNo, "Confirm") = vbYes Then
' If Directory does not exist, Create Directory under Payslips in the name of Month & Year
Dim fso As Object
Dim fldrpath As String
Set fso = CreateObject("scripting.filesystemobject")
fldrpath = Range("N16")
If Not fso.folderexists(fldrpath) Then
fso.createfolder (fldrpath)
End If
Range("A1:J43").Select
Pwd = Range("M13").Value
oPdf = Range("N17").Value & ".pdf"
fTemp = Range("N18").Value & "\" & "Temp.Pdf"
ActiveSheet.PageSetup.PrintArea = "$A$1:$J$43"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=fTemp, _
Quality:=xlQualityStandard
fTemp = """" & fTemp & """" 'Putting extra "" around for command Parameter.
oPdf = """" & oPdf & """"
Pwd = """" & Pwd & """"
'Making Command String for making protected PDFs Using PDFtk tool.
cmdStr = "pdftk " & fTemp & " Output " & oPdf & " User_pw " & Pwd & " Allow AllFeatures"
Shell cmdStr, vbHide 'Executing PDFtk Command.
Application.Wait DateAdd("s", 2, Now) 'Allowing 2 secs for command to execute.
' Kill Replace(fTemp, """", "")
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngdear As Range
Dim rngBody As Range
Dim rngAttach As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With ActiveSheet
Set rngTo = .Range("M12")
Set rngSubject = .Range("N20")
Set rngdear = .Range("N19")
Set rngBody = .Range("N21")
Set rngAttach = .Range("N22")
End With
SigString = "C:/Users/DianeTana/AppData/Roaming/Microsoft/Signatures/Internal.htm"
' If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
' Else
' Signature = ""
' End If
With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.HTMLBody = "<br>" & rngdear.Value & "<br><br>" & rngBody.Value & "<br><br>" & Signature
.Attachments.Add rngAttach.Value
.Save 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing
End If
End Sub
Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
Sub progress(pctCompl As Single)
UserForm1.Text.Caption = Format(pctCompl, "0") & "% Completed"
UserForm1.Bar.Width = pctCompl * 2
DoEvents
End Sub
Bookmarks