
Originally Posted by
tjmax
Hi Johnny,
Did you find anything? I am using Citrix and would like to print pdf in specific order as well. But I don't have any solution yet. Can you share with me the vba code if you find a solution? Thanks
Hi Tjmax,
as a matter of fact I did find a solution. Although it works you still have to do some tweaking because of the response time between the command sending and the printer response time.
Add rep points if this helps you 
Option Explicit
Declare PtrSafe Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Sub PrintPDf(fn$)
Dim pdfEXE$, q$
Dim ttest As String
Dim killPdf As Long
Dim ret
ttest = q & pdfEXE & q & " /s /o /h /t " & q & fn & q
pdfEXE = ExePath(fn)
If pdfEXE = "" Then
MsgBox "Path was not found.", vbCritical, "Oprire macro"
Exit Sub
End If
q = """"
'http://help.adobe.com/livedocs/acrobat_sdk/10/Acrobat10_HTMLHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Acrobat10_SDK_HTMLHelp&file=DevFAQ_UnderstandingSDK.22.31.html
'/s/o/h/p/t
Shell q & pdfEXE & q & " /s /o /h /t " & q & fn & q, vbHide
1: ret = IsFileOpen(fn)
If ret = True Then
GoTo 1
End If
End Sub
Function ExePath(lpFile As String) As String
Dim lpDirectory As String, sExePath As String, rc As Long
lpDirectory = "\"
sExePath = Space(255)
rc = FindExecutable(lpFile, lpDirectory, sExePath)
sExePath = Left$(sExePath, InStr(sExePath, Chr$(0)) - 1)
ExePath = sExePath
End Function
Public Sub PrintareAA()
Dim wksMenu As Worksheet ' e
Dim wksTable As Worksheet ' e
Dim wksParam As Worksheet ' e
Dim wksList As Worksheet ' e
Dim wksAAPrint As Worksheet ' e
Dim objFSO As Object ' e
Dim objFolder As Object ' e
Dim objFile As Object ' e
Dim i As Integer ' e
Dim tFolder As String ' e
Dim tFilePath As String ' e
Dim tFileName As String ' e
Dim lRow As Integer ' e
Dim k As Boolean ' e
Dim fn$
Dim iFileCount As Integer
Dim printName
Dim iPrintQueue As Integer
Dim folderspec As String
Dim ret
Application.EnableEvents = False
'You should adjust the code for your macro from here
Call StartOf ' personal macro - you should delete the line
Call UnBlock ' personal macro - you should delete the line
ThisWorkbook.Activate
Set wksMenu = Sheets("Menu") ' My sheet name - you should adjust or delete the line
Set wksTable = Sheets("Data") ' My sheet name - you should adjust or delete the line
Set wksParam = Sheets("Parametri") ' My sheet name - you should adjust or delete the line
Set wksList = Sheets("Lista") ' My sheet name - you should adjust or delete the line
Set wksAAPrint = Sheets("AA de printat") ' My sheet name - you should adjust or delete the line
'setting things up so I will get the name of the files from the folder
wksAAPrint.Activate
Cells.Select
Selection.Clear
wksAAPrint.Range("A1") = "Filename"
wksAAPrint.Range("B1") = "Path"
tFolder = wksMenu.Range("C22") ' location of the folder that contains the files to print
iPrintQueue = 0
folderspec = "C:\Windows\System32\spool\PRINTERS" ' location of the printer folder (should be the same to you)
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(tFolder)
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'print file name
wksAAPrint.Cells(i + 1, 1) = objFile.Name
'print file path
wksAAPrint.Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
'set printer
Call Change_Default_Printer ' this triggers another macro that asks the user to select the printer
'Print
k = False
lRow = wksAAPrint.Cells(Rows.Count, 1).End(xlUp).Row
'check spool printer - this is checking if the printer spool is empty so it can start the next job
iFileCount = 0
For i = 2 To lRow
tFilePath = wksAAPrint.Cells(i, 2)
tFileName = wksAAPrint.Cells(i, 1)
fn = tFilePath
Application.Wait Now + TimeValue("00:00:10") 'Wait 10 sec to send to print - you can adjust this depending on how fast you network and printer are - this requires allot of testing so I whould leave it like that
4: ret = IsFileOpen(fn)
If ret = True Then
GoTo 4
End If
'this prints the file 1 time if it has "A5" in the name, otherwise it's printing the file 2 times.
If InStr(tFileName, "A5") > 0 Then
PrintPDf fn
iFileCount = iFileCount + 1
Else
PrintPDf fn
PrintPDf fn
iFileCount = iFileCount + 1
End If
3: Next
Call Block ' personal macro - you should delete the line
Call EndOf ' personal macro - you should delete the line
wksMenu.Activate ' personal macro - you should adjust/delete the line
If iFileCount > 1 Then
MsgBox "A number of " & iFileCount & " files were printed"
Else
MsgBox iFileCount & " file was printed"
End If
End Sub
Sub Change_Default_Printer()
Dim sDefaultPrinter As String
Dim sSelectedPrinter As String
sDefaultPrinter = ActivePrinter
Application.Dialogs(xlDialogPrinterSetup).Show
sSelectedPrinter = ActivePrinter
Shell "RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n " & sSelectedPrinter
End Sub
Bookmarks