skuller Level: Trainee
 Registered: 25-05-2005 Posts: 3
|
Re: How can print PDF file automatically?
'IN this I am sending the path to the pdf file in my variable strFilePath. then will run through the code
Public Function Print_pdf_document(strFilePath As String)
On Error GoTo errHandler
Dim Error282Count As Integer '' Count of "Can't open DDE channel" errors
Dim AcroDDEFailed As Boolean '' Set to true if a DDE connection cannot be established
Dim sPDFPath As String '' Path to a PDF file
Dim sCmd As String '' DDE command
Dim lStatus As Long
Dim n As Integer
Const Max282Errors = 6
Dim sAcroPath As String ' Path to acrobat, determined by FindExecutable
Dim bCloseAcrobat As Boolean ' If we open acrobat, we will close it when we are done
Dim PDFArray As Variant
Error282Count = Max282Errors ' checks to see if acrobat is running
AcroDDEFailed = False ' ErrHandler will set to true if Acro is not running
txtAcrobatDDE.LinkMode = 0 ' Close any current DDE Link
txtAcrobatDDE.LinkTopic = "acroview|control" ' Acrobat's DDE Application|Topic
txtAcrobatDDE.LinkMode = 2 ' Try to establish 'Manual' DDE Link. This will fail
' if Acrobat is not ready or running
'This will determine what the default printer is on the system
Set objPrinter = GetDefaultPrinter()
'This will set the default printer according to the settings in TNClerk
Call SetNewDefaultPrinter
'Obtain the current settings the printer has
PreviousSettings = Printer.PaperSize
'Check to see if need to print legal or letter size
If Me.optLegal.Value = True Then
If Printer.PaperSize <> 5 Then
Call SetPaperSize(5)
End If
ElseIf Me.optLetter.Value = True Then
If Printer.PaperSize <> 1 Then
Call SetPaperSize(1)
End If
End If
ReDim PDFArray(0 To 0)
PDFArray(0) = strFilePath
If AcroDDEFailed = True Then
' grab the first pdf path. We assume this file exists
sPDFPath = PDFArray(0)
'' Use the FindExecutable API function to grab the path to our PDF handler.
sAcroPath = String(128, 32)
lStatus = FindExecutable(sPDFPath, vbNullString, sAcroPath)
If lStatus <= 32 Then
MsgBox "Acrobat could not be found on this computer. Printing cancelled", vbCritical, "Problem"
Call ReturnDefaultPrinter
Exit Function
End If
' Launch the PDF handler
lStatus = Shell(sAcroPath, vbHide)
If (lStatus >= 0) And (lStatus <= 32) Then
MsgBox "An error occured launching Acrobat. Printing cancelled", vbCritical, "Problem"
Call ReturnDefaultPrinter
Exit Function
End If
'Try to close Acrobat when we are done
bCloseAcrobat = True
End If
PauseFor 2 '' Lets take a break here to let Acrobat finish loading
Error282Count = 0 '' This time, we will allow all acceptable tries, as
AcroDDEFailed = False '' Acrobat is running, but may be busy loading its modules
txtAcrobatDDE.LinkMode = 0
txtAcrobatDDE.LinkTopic = "acroview|control"
txtAcrobatDDE.LinkTimeout = 2500 ' 3 minute timeout delay
txtAcrobatDDE.LinkMode = 2
If AcroDDEFailed = True Then
MsgBox "An error occured connecting to Acrobat. Printing cancelled", vbCritical, "Problem"
Call ReturnDefaultPrinter
Exit Function
End If
'' Send the PDF's to the printer.
For n = 0 To UBound(PDFArray)
'' We need to put the long filenames in quotes. Again, we assume these file exist
sPDFPath = PDFArray(n)
sCmd = "[FilePrintSilent(" & Chr(34) & sPDFPath & Chr(34) & ")]"
txtAcrobatDDE.LinkExecute sCmd
Next
If bCloseAcrobat = True Then
'' [AppExit()] causes memory errors with v6.0 and 6.1, so avoid closing these versions
If InStr(sAcroPath, "6.0") = 0 Then
sCmd = "[AppExit()]"
txtAcrobatDDE.LinkExecute sCmd
End If
End If
'' Close the DDE Connection
txtAcrobatDDE.LinkMode = 0
Call ReturnDefaultPrinter
Exit Function
errHandler:
If err.Number = 282 Then '' Can't open DDE channel
'' This error may happen because Acro is not fully loaded.
'' Give it Max282Errors attempts before returning AcroDDEFailed = True
Error282Count = Error282Count + 1
If Error282Count <= Max282Errors Then
PauseFor 3
Resume
Else
AcroDDEFailed = True
Resume Next
End If
End If
MsgBox "Error in PrintPDFs sub of " & Me.Name & " form. Error# " & err.Number & " " & err.Description & "."
End Function
|