How to Print PDF From Visual Basic
- 1). Open your VB editor within a Microsoft document by clicking "Tools," "Macros," "Visual Basic Editor."
- 2). Type the following code into the editor:
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
Replace "strPDFFileName" with the full name of your PDF document. Replace "C:\examplefile.pdf" with the directory location of your document. - 3). Type the following code to prepare your PDF file for printing:
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 - 4). Enter the below code to finally print your PDF document:
Sub CommandButton_Click()
Call OpenPDF
Call PrintPDF
End Sub
Source...