How to Print a PDF Using VBA

Written by katrina matterhorn
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Print a PDF Using VBA
Print PDF files with a few commands using VBA. (holding laptop image by Adam Borkowski from Fotolia.com)

Visual Basic Applications (VBA) designed by Microsoft use Visual 6 programming language. It was created for developers to enable user-defined functions and automate processes. It can be used to create a Word document from Excel and vice versa, as it is embedded in Microsoft Office applications. You can use VBA to print PDF files, if you understand the VBA language and editor.

Open your VBA editor with the document in which you wish to type the code. Click on "File," then hit "Tools." Click on "Macros." Hit "Visual Basic Editor." If using 2007 applications, click on "Developer," and then hit "Visual Basic."

Enter this code into the code window:

Sub OpenPDF()

Dim strPDFFileName As String

strPDFFileName = "C:\examplefile.pdf"

('This next function checks to see if the file isn't already open)

If Not FileLocked(strPDFFileName) Then

(If it returns False, then continue opening the PDF file)

Documents.Open strPDFFileName)

End If

End Sub

Edit "strPDFFileName" add the full file name to the PDF file that you want to open. Replace "C:\examplefile.pdf" with the actual location path of your PDF document.

Type in this code to prepare to print your PDF:

Sub PrintPDF (strPDFFileName as string)

Dim sAdobeReader as String

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

RetVal = Shell(sAdobeReader & "/P" & Chr(34) & sStrPDFFileName & Chr(34), 0)

End Sub

Modify the path in this code of the "Adobe Reader" to the actual path of your version.

Enter this code to trigger your PDF to print:

Sub CommandButton_Click()

Call OpenPDF

Call PrintPDF

End Sub

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.