why kill its self ?

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
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.

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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,223,714
Messages
6,174,052
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top