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:
Other People Are Reading
Open the document that you want to add this function to in one of the Microsoft Office applications.
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."
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."
Click on "Insert" and then "Module." Type in the code to open and then print PDF documents. The code looks like this:
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
'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)
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for