API Call to Close Another Application

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

Subject: API Call to Close Another Application

'(Declarations)Const WM_CLOSE = &H10

Const INFINITE = &HFFFFFFFF

Const vbNull = 0&

Declare Function WaitForSingleObject Lib “kernel32” (Byval hHandle As Long, Byval dwMilliseconds As Long) As Long

Declare Function FindWindow Lib “user32” Alias “FindWindowA” (Byval lpClassName As Long, 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, Byval lParam As Long) As Long

Dim hWindow As Variant

Dim lngReturnValue As Variant

Dim lngResult As Variant

'Calling it:

hWindow = FindWindow(vbNull, “(Untitled)”)

lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)

lngResult = WaitForSingleObject(hWindow, INFINITE)

Subject: RE: API Call to Close Another Application

Thanks so much. I’ll give it a try.