Using vba to print adobe pdf in a specific order, but adobe main program doesn't fully close

tjmax

New Member
Joined
Feb 16, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I am trying to write a vba code to print pdf in a specific order. However, if i just shell command the code will run too fast and the printing order will mess up. I saw online that someone recommend using Wscript.shell, since it has waitonreturn which will wait till the job is done before moving on the next vba code line. The problem is that adobe doesn't totally close after the first pdf was print. It close the pdf itself but adobe still keep running. I have to manually close the adobe after each printing so vba can continue with it's code. Is there any way to fix it? I will share my recent code here. I tried application.wait but it is not very stable because some pdf is big and some are small. I also try taskkill /f /im acrord32.exe but sometimes it will kill the next pdf print and unstable as well. I would like to find a stable way to do it. Great Thanks!

VBA Code:
Sub PrintPDF()
Dim DocName As String
Dim adobePath As String
Dim pAth As String
Dim i As Long

Dim wSh As Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1

Set wSh = VBA.CreateObject("WScript.Shell")
adobePath = """" & "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" & """"
pAth = Cells(4, 8).Value

i = 2
Do While Cells(i, 3) <> ""
DocName = Cells(i, 3).Value
wSh.Run adobePath & " /n /h /t " & """" & pAth & DocName & """", windowStyle, waitOnReturn
'Shell "taskkill /f /im acrord32.exe"

i = i + 1
Loop

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
To print the PDFs in the correct order you have to print each PDF and wait until it has finished spooling to the printer before printing the next PDF.

Have you tried setting the printer properties to print directly to the printer instead of spooling? That way, the PDFs should print in the order of the Shell commands, although I haven't tried this.

The problem with WScript.Shell Run with waitOnReturn:=True is that it waits until the command being run (the AcroRd32.exe process running the print command) has finished, not the print job. As you've found, you have to close the Adobe window manually to proceed with the next PDF.

The problem with Shell'ing the AcroRd32.exe command is that although the Shell commands are done in the correct order, the print spooler can change the order of printing, depending on the size of the PDFs.

You could add extra VBA code to close the Adobe/Acrobat window after each PDF has finished spooling, however it is a lot of code using the Windows API.

Another approach is running the PDFtk Server command-line tool's cat command using VBA code to catenate (merge) the multiple PDFs into a single temporary PDF and print that single file.
 
Upvote 0
To print the PDFs in the correct order you have to print each PDF and wait until it has finished spooling to the printer before printing the next PDF.

Have you tried setting the printer properties to print directly to the printer instead of spooling? That way, the PDFs should print in the order of the Shell commands, although I haven't tried this.

The problem with WScript.Shell Run with waitOnReturn:=True is that it waits until the command being run (the AcroRd32.exe process running the print command) has finished, not the print job. As you've found, you have to close the Adobe window manually to proceed with the next PDF.

The problem with Shell'ing the AcroRd32.exe command is that although the Shell commands are done in the correct order, the print spooler can change the order of printing, depending on the size of the PDFs.

You could add extra VBA code to close the Adobe/Acrobat window after each PDF has finished spooling, however it is a lot of code using the Windows API.

Another approach is running the PDFtk Server command-line tool's cat command using VBA code to catenate (merge) the multiple PDFs into a single temporary PDF and print that single file.
How do you write the code to set print properties to print directly instead of spooling?
 
Upvote 0
You don't write code, but manually set the printer properties via its properties dialogue. The 'Print directly to the printer' option is on the Advanced tab:

1647346341047.png
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top