Print PDF with page breaks VBA

2022

Board Regular
Joined
Jun 5, 2022
Messages
74
Office Version
  1. 2016
Platform
  1. Windows
I got this code from Chat GPT.

I asked it if it could give me code to print a PDF with page breaks.

But I don’t think this will do what I want it to.

For context, I’ve inherited a file which prints data in certain tabs as PDFS, but a user is also to select multiple tabs, so that one PDF with multiple pages (the different tabs) is printed.

One of the tabs has a document that sometimes caries in length. It’s usually one page, but is occasionally 5 pages, depending on the task.

What’s the best way to alter the data that is printed from that page, when it is 5 pages long? I assume I’d need to put in page breaks where I want the text to ‘break’ before it spills over to the next page?

Here’s the sample code from Chat GPT:

VBA Code:
Sub PrintPDFWithPageBreaks()

    ' Define the path and filename of the PDF file to print
    Dim filePath As String
    filePath = "C:\path\to\your\pdf\file.pdf"
    
    ' Open the PDF file
    Dim pdfApp As Object
    Set pdfApp = CreateObject("AcroExch.App")
    Dim pdfDoc As Object
    Set pdfDoc = CreateObject("AcroExch.PDDoc")
    pdfDoc.Open filePath
    
    ' Print the PDF file with page breaks
    pdfDoc.PrintPagesSilent 0, pdfDoc.GetNumPages - 1, 1, True
    
    ' Close the PDF file and exit
    pdfDoc.Close
    Set pdfDoc = Nothing
    Set pdfApp = Nothing
    
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is the code working for you, even if it doesn't do exactly what you want? Because it is referencing an Adobe Acrobat library that requires you have to Acrobat Professional installed.

Also I'm really unclear as to your question. This code just opens an existing PDF document and prints it, then closes it. Which is exactly what you asked for. What does this have to do with Excel? Do you want to export Excel sheets into a PDF file?
 
Upvote 0
Thank you for responding. I posted the question just before I went to bed and it’s got some typos - apologies!

I’ll re-post the question with a more relevant section of the code.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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