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