Forum Discussion
zedmekis
Feb 03, 2024Copper Contributor
print a pdf in specific order
Hi, I coded a excel to be able to import a number of information (client page num and so on) and the code order the client in a specific order then I made a button to send a print to the pdf to be able to print all pages in a specific order. the code is working the pages orinted in the right order but it didn't print the pdf even with the right path it print the excel files I would like to know where is the problem
here's the code for the VB :
Sub PrintPDFWithCustomOrder()
Dim pdfPrintOrder As Variant
pdfPrintOrder = Array(2, 1, 10, 4)
Dim pdfFilePath As String
pdfFilePath = "C:\Path\document.pdf"
Dim pdfApp As Object
Set pdfApp = CreateObject("AcroExch.App")
Dim pdfDoc As Object
Set pdfDoc = CreateObject("AcroExch.PDDoc")
If pdfDoc.Open(pdfFilePath) Then
For Each page In pdfPrintOrder
pdfDoc.PrintPages page, page
Next page
pdfDoc.Close
End If
Set pdfDoc = Nothing
Set pdfApp = Nothing
End Sub
1 Reply
Sort By
- NikolinoDEGold Contributor
It appears that you are attempting to use the Adobe Acrobat Automation (AcroExch) to print specific pages of a PDF file. However, there are a couple of issues with your code:
- Creating AcroExch Objects: The code you have provided attempts to create objects for AcroExch, but it seems to be incomplete. Additionally, the AcroExch automation is not always straightforward, and it's essential to check for the correct version of Acrobat installed on your machine.
- Using Shell to Print PDF: An alternative approach is to use the Shell function to execute a command-line print operation. This is a more common method and is simpler to implement.
Here is an example using Shell to print specific pages of a PDF file:
The VBA code is untested. Please back up your file in advance.
Sub PrintPDFWithCustomOrder() Dim pdfFilePath As String pdfFilePath = "C:\Path\document.pdf" ' Specify the pages you want to print in a comma-separated string Dim pdfPrintPages As String pdfPrintPages = "2,1,10,4" ' Construct the print command Dim printCommand As String printCommand = "AcroRd32.exe /N /T """ & pdfFilePath & """ ""Microsoft Print to PDF"" /p " & pdfPrintPages ' Use Shell to execute the print command Shell printCommand, vbHide End Sub
In this example, the command AcroRd32.exe /N /T ... is used to print the specified pages of the PDF file to the "Microsoft Print to PDF" printer. The vbHide argument in the Shell function hides the Adobe Reader window during printing.
Make sure to adjust the paths, file names, and page numbers according to your specific requirements. Additionally, ensure that the Adobe Reader is installed on your machine.
The text and steps were edited with the help of AI. AI can make mistakes. Consider checking important information.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.