[Solved] VBA to open pdf files in a folder and print all

bigme

Member
good day,
i have a big issue because i think is it possible to do this?
i have several pdf files in a specific folder, and i need to copy some of the files and save it to other folder then open all those saved files and print it, can VBA do this?
thank you.

regards,
bigMe
 
good day,
i have a big issue because i think is it possible to do this?
i have several pdf files in a specific folder, and i need to copy some of the files and save it to other folder then open all those saved files and print it, can VBA do this?
thank you.

regards,
bigMe
Hello BigMe

Thanks for sharing your problem with such clarity. You have several pdf files in a specific folder. You want to copy some of the files and save them to other folders, then open all those saved files using VBA.

Assuming your dataset is like the following GIF.
PDF Files in a Folder.gif

I am delighted to inform you that I have developed an Excel VBA Sub-procedure, a User-defined function and declared some required variables to fulfil your requirements.

Excel VBA Sub-procedure:
Code:
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
#End If

Sub PrintAndOpenPDFs()

    Dim fd As fileDialog
    Dim selectedFiles As FileDialogSelectedItems
    Dim sourceFolder As String
    Dim destinationFolder As String
    Dim destFilePath As String
    Dim i As Integer
    Dim SelectedFile As Variant

    Set fd = Application.fileDialog(msoFileDialogFilePicker)
    fd.Title = "Select PDF files"
    fd.Filters.Clear
    fd.Filters.Add "PDF files", "*.pdf"

    If fd.Show = -1 Then

        Set selectedFiles = fd.SelectedItems

        sourceFolder = GetFolderFromPath(selectedFiles(1))

        destinationFolder = sourceFolder & "\Copied_PDFs_" & Format(Now, "yyyymmdd_hhmmss") & "\"

        MkDir destinationFolder

        For Each SelectedFile In selectedFiles
            destFilePath = destinationFolder & "\" & Mid(SelectedFile, InStrRev(SelectedFile, "\") + 1)
            FileCopy SelectedFile, destFilePath
        Next SelectedFile

        For i = 1 To selectedFiles.Count
            destFilePath = destinationFolder & "\" & Mid(selectedFiles(i), InStrRev(selectedFiles(i), "\") + 1)
            Shell "print " & destFilePath, vbNormalFocus
            ShellExecute 0, "open", destFilePath, vbNullString, vbNullString, vbNormalFocus
        Next i

        MsgBox "Printing and opening completed successfully.", vbInformation
    
    Else
        MsgBox "No files selected.", vbExclamation
    End If

End Sub

Function GetFolderFromPath(ByVal fullPath As String) As String

    GetFolderFromPath = Left(fullPath, InStrRev(fullPath, "\"))

End Function

OUTPUT OVERVIEW:
Output of running Excel VBA code.gif

I am also attaching the solution workbook. Hopefully, the idea will fulfil your goal. Good luck!

Regards
Lutfor Rahman Shimanto
ExcelDemy
 

Attachments

Online statistics

Members online
0
Guests online
6
Total visitors
6

Forum statistics

Threads
371
Messages
1,627
Members
705
Latest member
curioso
Back
Top