Print a PDF File (1 Viewer)

Jmsteph

Registered User.
Local time
Yesterday, 21:37
Joined
Dec 2, 2008
Messages
28
Hello,

I am using a form for rejections and want to add a check box so that when I click the command button at the end it will print a PDF file that is on our shared drive. I have searched the web for different document, but can't seem to find any code that works. Does anyone have any suggestions?
Thanks,

JS
 

HiTechCoach

Well-known member
Local time
Yesterday, 21:37
Joined
Mar 6, 2006
Messages
4,357
What code have you tried?

What PDF software are you wanting to use to for printing?
 

ghudson

Registered User.
Local time
Yesterday, 22:37
Joined
Jun 8, 2002
Messages
6,195
There is a free add-in download for the PDF XPS output @ microsoft.com which works with Access 2007.
 

HiTechCoach

Well-known member
Local time
Yesterday, 21:37
Joined
Mar 6, 2006
Messages
4,357
There is a free add-in download for the PDF XPS output @ microsoft.com which works with Access 2007.

the problem is not creating the PDF, but printing a already existing PDF
 

ghudson

Registered User.
Local time
Yesterday, 22:37
Joined
Jun 8, 2002
Messages
6,195
Curious, have you actually tried printing a PDF with you example code?

Does Acrobat close after printing?

I admit that I had not, until you asked. Only used the code for printing Office files which close after printed. As you noticed, Adobe does not close after printing a PDF using the code I mentioned. ;-)

Here is some code that will close an open application... http://www.mvps.org/access/api/api0025.htm
 

Jmsteph

Registered User.
Local time
Yesterday, 21:37
Joined
Dec 2, 2008
Messages
28
I tried to use the below link which looks pretty helpful. The challenge is I don't know the classname for the PDF so I tried using the code to locate the class name, but it won't print out the list. I will admit I am not very knowledgable in this area of access.

http://www.mvps.org/access/api/api0025.htm
 

HiTechCoach

Well-known member
Local time
Yesterday, 21:37
Joined
Mar 6, 2006
Messages
4,357
I admit that I had not, until you asked. Only used the code for printing Office files which close after printed. As you noticed, Adobe does not close after printing a PDF using the code I mentioned. ;-)

Here is some code that will close an open application... [/QUOTE] curious again. Have you been successful with that code closing acrobat?
 

ghudson

Registered User.
Local time
Yesterday, 22:37
Joined
Jun 8, 2002
Messages
6,195
curious again. Have you been successful with that code closing acrobat?

I admit that it has been years since I messed with the above mentioned API's. Have you tried it? ;-) I took your challenge to prove that my suggestion works and just ran it without any problems [printed my pdf and closed adobe]. I am using Adobe Acrobat Professional so my window name = AcrobatSDIWindow. Adobe Reader will have a different window name. I will include the code below for those interested. I put each API into its own module.

Code:
Module: dTestPrint
Option Compare Database
Option Explicit

'OpenPrintFile()
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Public Function PrintFile(sFile As String)
On Error GoTo Err_PrintFile

    PrintFile = ShellExecute(Application.hWndAccessApp, "Print", sFile, "", "", 0) '0 = SW_HIDE, 1 = SW_NORMAL, 2 = SW_SHOWMINIMIZED, 3 = SW_MAXIMIZE, 4 = SW_SHOWNOACTIVATE, 6 = SW_MINIMIZE, 7 = SW_SHOWMINNOACTIVE, 9 = SW_RESTORE
  
Exit_PrintFile:
    Exit Function

Err_PrintFile:
    MsgBox Err.Number & " - " & Err.Description, vbCritical, "PrintFile()"
    Resume Exit_PrintFile
  
End Function

Public Sub PrintTheFile()
 
    Call PrintFile("C:\Testing\Sample.pdf")
    Call fCloseApp("AcrobatSDIWindow") 'Adobe Acrobat Professional
    MsgBox "Printed file and closed Adobe."
 
End Sub

Code:
Module: dFindClassNameOfRunningApp
Option Compare Database
Option Explicit

'[URL="http://www.mvps.org/access/api/api0013.htm"]http://www.mvps.org/access/api/api0013.htm[/URL]
'************** Code Start ***************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
'(Q)    How do I find out the Class Name of a running application?
'(A)    Paste the following code in a new module and then run the function fEnumWindows from the Debug window. The function will print out the Class Name and Caption of all running applications that are currently visible.
'
Private Declare Function apiGetClassName Lib "user32" Alias _
                "GetClassNameA" (ByVal hWnd As Long, _
                ByVal lpClassName As String, _
                ByVal nMaxCount As Long) As Long
Private Declare Function apiGetDesktopWindow Lib "user32" Alias _
                "GetDesktopWindow" () As Long
Private Declare Function apiGetWindow Lib "user32" Alias _
                "GetWindow" (ByVal hWnd As Long, _
                ByVal wCmd As Long) As Long
Private Declare Function apiGetWindowLong Lib "user32" Alias _
                "GetWindowLongA" (ByVal hWnd As Long, ByVal _
                nIndex As Long) As Long
Private Declare Function apiGetWindowText Lib "user32" Alias _
                "GetWindowTextA" (ByVal hWnd As Long, ByVal _
                lpString As String, ByVal aint As Long) As Long
Private Const mcGWCHILD = 5
Private Const mcGWHWNDNEXT = 2
Private Const mcGWLSTYLE = (-16)
Private Const mcWSVISIBLE = &H10000000
Private Const mconMAXLEN = 255

Function fEnumWindows()
Dim lngx As Long, lngLen As Long
Dim lngStyle As Long, strCaption As String
    
    lngx = apiGetDesktopWindow()
    'Return the first child to Desktop
    lngx = apiGetWindow(lngx, mcGWCHILD)
    
    Do While Not lngx = 0
        strCaption = fGetCaption(lngx)
        If Len(strCaption) > 0 Then
            lngStyle = apiGetWindowLong(lngx, mcGWLSTYLE)
            'enum visible windows only
            If lngStyle And mcWSVISIBLE Then
                Debug.Print "Class = " & fGetClassName(lngx),
                Debug.Print "Caption = " & fGetCaption(lngx)
            End If
        End If
        lngx = apiGetWindow(lngx, mcGWHWNDNEXT)
    Loop
End Function
Private Function fGetClassName(hWnd As Long) As String
    Dim strBuffer As String
    Dim intCount As Integer
   
    strBuffer = String$(mconMAXLEN - 1, 0)
    intCount = apiGetClassName(hWnd, strBuffer, mconMAXLEN)
    If intCount > 0 Then
        fGetClassName = Left$(strBuffer, intCount)
    End If
End Function

Private Function fGetCaption(hWnd As Long) As String
    Dim strBuffer As String
    Dim intCount As Integer

    strBuffer = String$(mconMAXLEN - 1, 0)
    intCount = apiGetWindowText(hWnd, strBuffer, mconMAXLEN)
    If intCount > 0 Then
        fGetCaption = Left$(strBuffer, intCount)
    End If
End Function
'************** Code End ***************

Code:
Module: dCloseAnotherApplication
Option Compare Database
Option Explicit

'[URL="http://www.mvps.org/access/api/api0025.htm"]http://www.mvps.org/access/api/api0025.htm[/URL]
'************** Code Start ***************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Const WM_CLOSE = &H10
Private Const INFINITE = &HFFFFFFFF

Private Declare Function apiPostMessage _
    Lib "user32" Alias "PostMessageA" _
    (ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) _
    As Long

Private Declare Function apiFindWindow _
    Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) _
    As Long
    
Private Declare Function apiWaitForSingleObject _
    Lib "kernel32" Alias "WaitForSingleObject" _
    (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) _
    As Long
    
Private Declare Function apiIsWindow _
    Lib "user32" Alias "IsWindow" _
    (ByVal hWnd As Long) _
    As Long
        
Private Declare Function apiGetWindowThreadProcessId _
    Lib "user32" Alias "GetWindowThreadProcessId" _
    (ByVal hWnd As Long, _
    lpdwProcessID As Long) _
    As Long
        
Function fCloseApp(lpClassName As String) As Boolean
'Usage Examples:
'   To close Calculator:
'       ?fCloseApp("SciCalc")
'
Dim lngRet As Long, hWnd As Long, pID As Long

    hWnd = apiFindWindow(lpClassName, vbNullString)
    If (hWnd) Then
        lngRet = apiPostMessage(hWnd, WM_CLOSE, 0, ByVal 0&)
        Call apiGetWindowThreadProcessId(hWnd, pID)
        Call apiWaitForSingleObject(pID, INFINITE)
        fCloseApp = Not (apiIsWindow(hWnd) = 0)
    End If
End Function
'************* Code End ***************
 

boblarson

Smeghead
Local time
Yesterday, 19:37
Joined
Jan 12, 2001
Messages
32,059
What if you have more than one Acrobat Reader window open. Will it close everything?

I'm struggling to find a way to print the pdfs I created using some other code which eventually I'll post. But we used to use ACG Soft's PDF PRO PLUS Library which allowed for a print after merging, but we have to move off of that as it doesn't work on Citrix.
 

Users who are viewing this thread

Top Bottom