Results 1 to 1 of 1

After VBA functions run. I get #N/A

Threaded View

  1. #1
    Registered User
    Join Date
    10-22-2017
    Location
    Dubai
    MS-Off Ver
    2016
    Posts
    1

    After VBA functions run. I get #N/A

    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
    Last edited by Leith Ross; 02-11-2018 at 04:21 PM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 9
    Last Post: 06-13-2017, 01:41 PM
  2. Replies: 6
    Last Post: 03-17-2015, 01:35 AM
  3. Which Excel functions work in user-defined functions ?
    By RogeratCCCC in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 04-28-2012, 06:47 PM
  4. Replies: 1
    Last Post: 02-10-2012, 05:27 PM
  5. Replies: 0
    Last Post: 11-15-2007, 05:24 AM
  6. Replies: 2
    Last Post: 07-13-2006, 11:30 PM
  7. [SOLVED] Conversion from Spreadsheet Toolkit functions (ESSV....) to EssBase API functions
    By sujay in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-05-2006, 05:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1