How to Print PDF File With VBA?

Written by tiesha whatley
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Print PDF File With VBA?
Open and print PDF documents programmatically from inside an Office application. (Thomas Northcut/Photodisc/Getty Images)

Visual Basic for Applications, or VBA, is the programming language Microsoft included with all of its Office programs. It is derived from the classic Basic programming language and is used to create dynamic Microsoft Office documents. You can communicate with other programs, including Adobe Acrobat or Reader, to open and then print documents right inside an Office application. At this time, Adobe Acrobat and Reader are the only PDF readers that communicate easily within Visual Basic for Applications.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Open the document that you want to add this function to in one of the Microsoft Office applications.

  2. 2

    Show the "Developer" tab in the application, except for Access, if it isn't already showing. Click on the "File" tab and then "Options." Click on "Customize Ribbons." Under "Main Tabs" click on the checkbox next to "Developer." Click on "OK."

  3. 3

    Open the Visual Basic Editor. In Access, click on the "Database Tools" tab and then "Visual Basic." In other programs, click on the "Developer" tab and then "Visual Basic."

  4. 4

    Click on "Insert" and then "Module." Type in the code to open and then print PDF documents. The code looks like this:

    "Sub PrintPDF()

    Dim FilePath as string
    
    Dim appPDF as string
    
    
    
    'The PDF file you want to open
    
    strFilePath = "C:\Documents\sample.pdf"
    

    'Check to see if the file is open already

    If Not FileLocked(strFilePath) then

       Documents.Open strFilePath
    

    End If

    'Now that the file is open you can print it

    'first set the variable to the Adobe program on your computer

    appPDF = "C:\Program Files\Adobe\Acrobat 6.0\Reader\AcroRd32.exe"

    'Now print the file

    RetVal = Shell(appPDF & "/P" & Chr(34) & strFilePath & Chr(34), 0)

    End Sub"

  5. 5

    Click the "Save" button in the Visual Basic Editor to save the module.

Tips and warnings

  • You can copy and paste the entire function in Step 4 that starts with "Sub" and ends with "End Sub" into the code editor to print a PDF document using VBA.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.