Option Compare Database
Option Explicit
'Written by Andrew Fergus
'set a reference to Windows Script Host Object Model
Private Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
Private 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
Const SW_SHOWNORMAL = 1
Private Sub PrintDoc_Click()
Dim dfltPrinter As String
Dim newPrinter As New WshNetwork
'Get the name of the default printer
dfltPrinter = Me.Printer.DeviceName
'Set the default to the new printer
'USE YOUR ACTUAL PRINTER NAME BELOW
newPrinter.SetDefaultPrinter ("New Printer Name")
'Pause
Sleep 100
'Print the pdf document
'USE YOUR ACTUAL DOCUMENT NAME & PATH BELOW
Call ShellExecute(Me.hwnd, "print", "c:\MyDocument.pdf", "", 0, SW_SHOWNORMAL)
'Pause
Sleep 2000 'wait for 2 seconds - increase this if the code doesn't appear to work
'kill the acrobat process, if it is still running
KillProcess ("acrobat")
'Pause
Sleep 100
'Set the printer back to the original default
newPrinter.SetDefaultPrinter (dfltPrinter)
End Sub
Public Sub KillProcess(ByVal processName As String)
Dim oWMI As Variant
Dim ret As Long
Dim sService As Variant
Dim oWMIServices As Variant
Dim oWMIService As Variant
Dim oServices As Variant
Dim oService As Variant
Dim servicename As String
Set oWMI = GetObject("winmgmts:")
Set oServices = oWMI.InstancesOf("win32_process")
For Each oService In oServices
servicename = LCase(Trim(CStr(oService.Name) & ""))
If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
ret = oService.Terminate
End If
Next
Set oServices = Nothing
Set oWMI = Nothing
End Sub