I’m using the following code to loop through unprocessed documents, open attachments and print them. It works great except that once the documents are printed, I would like to close the application whether it be Word or Excel or whatever.
Does anyone know the API command that would close the application? Call TerminateProcess doesn’t do it (it closes the attachment but leaves the called ap up and running). I’ve been searching for a while now and can’t find anything.
Here is a code snippet:
(Declarations)
Const SEE_MASK_NOCLOSEPROCESS = &H40
Const SEE_MASK_FLAG_NO_UI = &H400
Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Declare Function ShellExecuteEx Lib “shell32.dll”_
Alias “ShellExecuteEx” (SEI As SHELLEXECUTEINFO) As Long
Declare Function TerminateProcess Lib “kernel32”_
Alias “TerminateProcess” (Byval hProcess As Long, Byval uExitCode As Long) As Long
Sub Initialize
Dim SEI As SHELLEXECUTEINFO
SEI.cbSize = Len(SEI)
SEI.fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_FLAG_NO_UI
SEI.lpVerb = “open”
SEI.lpFile = filepath 'gets the attachment name
SEI.nShow = 1
SEI.hInstApp = 0
SEI.lpIDList = 0
Call ShellExecuteEx(SEI)
SIE.lpVerb = “print”
Call ShellExecuteEx(SEI)
Call TerminateProcess(SEI.hProcess, 0) 'closes the document but not the application that it printed from
End Sub