Option Compare Database
Option Explicit
Private Declare Function FindExecutable Lib "shell32.dll" _
Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, _
ByVal lpResult As String) As Long
Public Function PrintPDF(FileName As Variant) As Boolean
On Error GoTo ErrHandler
Dim Error282Count As Integer
Dim AcroDDEFailed As Boolean
Dim strCmd As String
Dim lStatus As Long
Const Max282Errors = 6
Static strAcroPath As String
Dim bCloseAcrobat As Boolean
Error282Count = Max282Errors
AcroDDEFailed = False
Dim lngChanel As Long
lngChanel = DDEInitiate("acroview", "control")
If AcroDDEFailed = True Then
If Len(strAcroPath) = 0 Then
strAcroPath = String(128, 32)
lStatus = FindExecutable(FileName, vbNullString, strAcroPath)
If lStatus <= 32 Then
MsgBox "Acrobat could not be found on this computer. Printing cancelled", vbCritical, "Problem"
Exit Function
End If
End If
lStatus = Shell(strAcroPath, vbHide)
If (lStatus >= 0) And (lStatus <= 32) Then
MsgBox "An error occured launching Acrobat. Printing cancelled", vbCritical, "Problem"
Exit Function
End If
bCloseAcrobat = True
End If
PauseFor 2
Error282Count = 0
AcroDDEFailed = False
lngChanel = DDEInitiate("acroview", "control")
If AcroDDEFailed = True Then
MsgBox "An error occured connecting to Acrobat. Printing cancelled", vbCritical, "Problem"
Exit Function
End If
strCmd = "[FilePrintSilent(" & Chr(34) & FileName & Chr(34) & ")]"
DDEExecute lngChanel, strCmd
PrintPDF = True
If bCloseAcrobat = True Then
If InStr(strAcroPath, "6.0") = 0 Then
strCmd = "[AppExit()]"
DDEExecute lngChanel, strCmd
End If
End If
DDETerminateAll
Exit Function
ErrHandler:
If Err.Number = 282 Then
Error282Count = Error282Count + 1
If Error282Count <= Max282Errors Then
PauseFor 3
Resume
Else
AcroDDEFailed = True
Resume Next
End If
End If
MsgBox "Error in PrintPDF sub Error# " & Err.Number & " " & Err.Description & "."
End Function
Private Sub PauseFor(iSeconds As Integer)
Dim sngTimer As Single
sngTimer = Timer
While Timer - sngTimer < iSeconds
DoEvents
Wend
End Sub