Link to home
Start Free TrialLog in
Avatar of MariaHalt
MariaHaltFlag for United States of America

asked on

Print PDF files through VB app directly to printer and then close Adobe

I have a list box (style = checkbox) that lists different pdf files.  When the command button is clicked it loops through the list box items, looking for checked items and then prints those files directly to the printer using a ShellExecute command.  The ShellExecute command opens Adobe Reader.  How can I close Adobe Reader after all the files have printed?  The app will run on several machines all with different versions of Adobe Reader no doubt.  So the answer must address this as well.  Thanks.
Sub cmdPrintPDF()
 
For i = 0 To lstApplications.ListCount - 1
    If lstApplications.Selected(i) = True Then
                ShellExecute Me.hwnd, "print",  lstApplications.List(i), vbNullString, vbNullString, vbHide
    End If
Next i
 
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ladarling
ladarling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Corection,: that URL shows ShellExecute, you will need to use ShellExecuteEx, a good example is actually here:
http://www.tech-archive.net/Archive/VC/microsoft.public.vc.mfc/2006-04/msg01244.html
 
Avatar of MariaHalt

ASKER

This did lead me down the right track.  I wrote a routine called CloseWindow.  It uses the API calls FindWindow and PostMessage to close the Adobe Reader window.

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Const WM_CLOSE = &H10

Sub CloseWindow(ByVal strWindowName As String)
Dim lngWindowHandle As Long

lngWindowHandle = FindWindow(vbNullString, strWindowName)

If lngWindowHandle <> 0 Then
    'Close the window
    PostMessage lngWindowHandle, WM_CLOSE, 0&, 0&
End If

End Sub


Sub cmdClick()

CloseWindow ("Adobe Reader")

End Sub