How to Print a PDF Using VBA

Updated April 17, 2017

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

bibliography-icon icon for annotation tool Cite this Article

About the Author

Katrina Matterhorn is experienced in technology consulting for two of the largest technology companies in the world. Since 2009 she has specialized in technology, business and home and garden articles. She has a double major in English and political science from Austin College in addition to Microsoft and IBM certifications.