this modified code from other Site
the code below kill specific applications as you want and work well
My problem with Excel Apps if this Work Book open next others Excel it kill it self while the hWndThis for SendMessage hWndThis, WM_SYSCOMMAND, SC_CLOSE, 0& belong for another one.
the code below kill specific applications as you want and work well
My problem with Excel Apps if this Work Book open next others Excel it kill it self while the hWndThis for SendMessage hWndThis, WM_SYSCOMMAND, SC_CLOSE, 0& belong for another one.
Code:
Private Const GW_HWNDNEXT = 2Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Sub Kill_APPs()
[B]Call Kill_Task("*.xls*")' excel[/B]
[COLOR=#008000]' Call Kill_Task("*.txt*")' Notepad
' Call Kill_Task("*.docx*")' Word[/COLOR]
End Sub
Sub Kill_Task(Optional Title = "*", Optional Class = "*")
Dim proc As Object
Dim hWndThis As Long
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060
hWndThis = FindWindow(vbNullString, vbNullString)
While hWndThis
PID = ""
Dim sTitle As String, sClass As String
sTitle = Space$(255)
sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))
sClass = Space$(255)
sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))
'' if need this is PID
'PID = GetWindowThreadProcessId(hWndThis, PID)
If (sTitle Like Title And sClass Like Class) And InStr(sTitle, ThisWorkbook.Name) = 0 Then
SendMessage hWndThis, WM_SYSCOMMAND, SC_CLOSE, 0&
End If
hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
Wend
End Sub