简体   繁体   中英

VBA - Macro - To Print Multiple digital signed PDF file and save it in subfolder by using “Microsoft Print to PDF” Printer

I am printing multiple digital signed PDF file into PDF via "Microsoft print to PDF" ( To Edit document) . Below mention VBA code is working perfectly. But when run this code each time, it is asking Filename & Destination folder for printed file.

My Expection:

It has to capture file name from existing saved documents file name and destination folder path we have include in VBA Code. Please help me, How to solve this

Public Sub Print_All_PDF_Files_in_Folder()
    Dim folder As String
    Dim PDFfilename As String
    
    folder = "C:\Users\Desktop\VBA\"    '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

Path with spaces must be in quotes, because it is has spaces. Keys /p and /h must be separate from Program name. I check it this way: i make this command in cmd.exe and when i see what it correct - I revrite it into macro.

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

You seem to have multiple conflicts

Your command includes the command to open the Printer Dialog

/P <filename> - Open and go straight to the Printer Prompt dialog

And for "Microsoft Print to PDF" that will allow you to make the manual changes you require to the PDF then manually save to a folder or filename of your choosing.

However you say you want Acrobat to save to a known filename without that prompting. Which in turn makes me question WHY are you using Acrobat to open a PDF and re-save it as a file name without interaction ?

You could do that simply by renaming the PDF without opening it in Acrobat.

One advantage of programmatically opening a "Complex" PDF in Acrobat and Re-Printing as a "Dumber" PDF using "Microsoft Print to PDF" is it can pseudo-manically emulate much more efficient ways of flattening by using a very inefficient reprinting and for that you need to use:-

/T <filename> <printername> <drivername> <portname> - Print the file on the specified printer.

Where printername and drivername are "Microsoft Print to PDF" and portname is where you want it printed.

There are much lighter ways to process a PDF from the command line, but if you already have installed heavyweight Adobe Reader then this is the defacto standard.

[EDIT] in the comments you imply you still need to use acrobat for processing before printing to a fixed name. Then in that case, you need to run those actions first. Before saving as new PDF, prior to printing, thus you need to

  1. get filename
  2. make changes
  3. save changes as new filename
  4. send new filename to printer using:-
"C:\Full path\to\AcroRd32.exe" /T "C:\path to\Input.pdf" "Microsoft Print to PDF" "Microsoft Print to PDF" "C:\path to\Output.pdf"

The problem with batch printing, using /T = TSR (Terminate and Stay Resident), is that the window stays open waiting for the next print in the batch , and most users then add /H to hide it, then afterwards complain its not accessible so as to close at the end of the batch (which simply requires sendkeys %FX or Alt+F4 to close the open window)! One way round that is, on the last print invoke /T without H , and then a VB focused command (object.AppActivate title) and at simplest sendkeys %FX will close the window.

If using the command line or a .cmd it is simple to use Wscript with a single line .VBS command, however in this case you are already using VBA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM