Print_All_PDF_Files_in_Folder()

146 Views Asked by At

I've been using this code to print PDFs from a folder at work but the code isn't working anymore. I'm working remotely from home and I've updated the file path and I still receive a run time 53 error code. Can anyone help?

Public Sub Print_All_PDF_Files_in_Folder()
    Dim folder As String
    Dim PDFfilename As String
    
    folder = "C:\Users\16468\Desktop\CONF\TAXES"    'CHANGE AS REQUIRED
    If Right(folder, 1) <> "\" Then folder = folder & "\"
       
    PDFfilename = Dir(folder & "*.pdf", vbNormal)
    While Len(PDFfilename) <> 0
        Print_PDF folder & PDFfilename
        PDFfilename = Dir()  ' Get next matching file
    Wend
End Sub

Private Sub Print_PDF(sPDFfile As String)
    Shell "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe /p /h " & Chr(34) & sPDFfile & Chr(34), vbNormalFocus
End Sub
1

There are 1 best solutions below

0
Cameron Critchlow On

This Should print just about anything:

Option Explicit
#If VBA7 Then
    Private Declare PtrSafe Function ShellExecute _
        Lib "shell32.dll" Alias "ShellExecuteA" _
            (ByVal hwnd As LongPtr, _
             ByVal lpOperation As String, _
             ByVal lpFile As String, _
             ByVal lpParameters As String, _
             ByVal lpDirectory As String, _
             ByVal nShowCmd As Long) _
        As LongPtr
#Else
    Private Declare Function ShellExecute _
        Lib "shell32.dll" Alias "ShellExecuteA" _
            (ByVal hwnd As Long, _
             ByVal lpOperation As String, _
             ByVal lpFile As String, _
             ByVal lpParameters As String, _
             ByVal lpDirectory As String, _
            ByVal nShowCmd As Long) _
        As Long
#End If
Private Const SW_HIDE = 0
Sub FilePrint(ByVal strFilePath As String)

    Dim retVal As Long
    
        retVal = ShellExecute(0, "Print", strFilePath, 0, 0, SW_HIDE)

        If retVal < 32 Then
           '// there are Error codes for this..left out
            MsgBox "An Error occured...could not print"
        End If

End Sub
Public Sub Print_All_PDF_Files_in_Folder()
    Dim folder As String
    Dim PDFfilename As String
    
    folder = "C:\Users\16468\Desktop\CONF\TAXES"    'CHANGE AS REQUIRED
    If Right(folder, 1) <> "\" Then folder = folder & "\"
       
    PDFfilename = Dir(folder & "*.pdf", vbNormal)
    While Len(PDFfilename) <> 0
        FilePrint folder & PDFfilename
        PDFfilename = Dir()  ' Get next matching file
    Wend
End Sub

Original Code found here