@Poremsky.com

Tips & Tricks for Windows, Office, and Whatever

  • Home
  • Windows
  • Office
  • BlackBerry
  • Web Design
  • WordPress
  • Internet Explorer

Print a PDF using VBA

Published on February 6, 2015 Last reviewed on January 14, 2023

I'm often asked how to control print settings when using a macro to print from Outlook. In most cases you can't, because the print dialog is not exposed in the object model. You can use Windows Shell to print any document type, using the native application, assuming you have an application installed that can open and print the file type, but you can't set options, such as page numbers, using Windows Shell.

If the application supports OLE, you may be able to control some aspects of the printout. For example, if you are printing a PDF and have Adobe Acrobat installed, you can use the PrintPages function in the Acrobat object model to print selected pages and shrink to fit the page.

Function PrintPages(nFirstPage As Long, nLastPage As Long, nPSLevel As Long, bBinaryOk As Long, bShrinkToFit As Long) As Boolean

Call the function in the macro using this format:
Call AcroExchAVDoc.PrintPages(0, 1, 2, 1, 1)

The page count begins with 0, not 1, so to print the first 3 pages, you'd use 0 and 2 as the first 2 values:
Call AcroExchAVDoc.PrintPages(0, 2, 2, 1, 1)

To print only page 2, use 1 and 1 as the first 2 values:
Call AcroExchAVDoc.PrintPages(1, 1, 2, 1, 1)

To use this code in Office applications, you need to set a reference to Acrobat in the VB Editor's Tools, References dialog box.

In the sample code below, pass the FileName and optional PrintMode values to the macro using

AcrobatPrint "c:\My Documents\my-file.pdf", "All"
or
AcrobatPrint "c:\My Documents\my-file.pdf"

Public Sub AcrobatPrint(FileName As String, PrintMode As String)

     Dim AcroExchApp As Acrobat.CAcroApp
     Dim AcroExchAVDoc As Acrobat.CAcroAVDoc
     Dim AcroExchPDDoc As Acrobat.CAcroPDDoc
     Dim num As Integer

     Set AcroExchApp = CreateObject("AcroExch.App")
     Set AcroExchAVDoc = CreateObject("AcroExch.AVDoc")

     ' Open the [Filename] pdf file
     AcroExchAVDoc.Open FileName, ""

     'Get the PDDoc associated with the open AVDoc
     Set AcroExchPDDoc = AcroExchAVDoc.GetPDDoc

     'Get the number of pages for this pdf
     num = AcroExchPDDoc.GetNumPages - 1

     If PrintMode = "All" Then

          'Print Entire Document
           Call AcroExchAVDoc.PrintPages(0, num, 1, 1, 1)
     Else
           If num = 0 Then
               'Print one page document
                Call AcroExchAVDoc.PrintPages(0, num, 2, 1, 1)
           Else
               'Print first two pages
                Call AcroExchAVDoc.PrintPages(0, 1, 1, 1, 1)
           End If
     End If
  
     AcroExchApp.Exit
     AcroExchAVDoc.Close (True)
     AcroExchPDDoc.Close

End Sub

More Information

Developing Applications Using Interapplication Communication

Share this:

  • Facebook
  • LinkedIn
  • Twitter
  • Print
Subscribe
Notify of
4 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Markus Rinninger
May 4, 2020 3:45 am
#387

Starting with version 7, the third parameter of printPages() (PSLevel) must be set to 2 or 3.
1 seems not to be allowed anymore.

0
Reply
Helen Feddema
July 26, 2016 1:34 pm
#253

I have not had success with this method. The line that should print the PDF does not print, with no error messages. I tried it on two computers, both running Acrobat XI Standard, and I also tried using the old SendKeys method, without success. Do you have any suggestions? I tried to paste my code, but it is too long. I am running 64-bit Windows 10, and my code runs from Access 2010 (with a reference to the Outlook and Acrobat object models). The code saves PDF attachments from Outlook mail messages and then attempts to print them using your… Read more »

8
Reply
John GENDERS
February 1, 2016 6:31 am
#232

Hi, this was very useful. Well it would if I could actually obtain a printed copy 🙂
So a few questions associated with it:
How do I state the printer to use? Must it be the default?
How come nothing actually comes out of my default printer ... or indeed any printer ?
Thanks for any help you can give.

1
Reply
Diane Poremsky(@dporemsky)
Author
Reply to  John GENDERS
February 8, 2016 8:16 am
#233

This macro is actually creating a pdf, not sending one to a paper printer. But... when you are printing to paper from a macro, you would need to call up windows api to change the printer (i don't have any code samples for that). To use the default printer, declare Private Declare Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long then use ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0 in your macro. (a… Read more »

-1
Reply

Recent Posts

  • Change Password or Pin Complexity after removing from Intune
  • Server drafts location is not valid error message
  • What is the Brown File Box Icon on Files in Windows 11?
  • Remove Office Licenses from Windows
  • Add the Recent Items Folder to Windows 10 Quick Access

Recent Comments

  • FUAD on Remove Office Licenses from Windows
  • Brian DuBridge on Add the Recent Items Folder to Windows 10 Quick Access
  • Renee Moodie on Remove Office Licenses from Windows
  • John P on Remove Office Licenses from Windows
  • Puja on Remove Office Licenses from Windows
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

© 2023 · @Poremsky.com

wpDiscuz
Go to mobile version
You are going to send email to

Move Comment