Loop to Close Notepad using API By Typ "Notepad"

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
Greeting All
could be using Loop to close All NotePad, Here I click "Sub KillNotepadOneByOne()" this Macro Sub till I close All notepad

Thanks
Code:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private 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
Sub KillNotepadOneByOne()
    Dim hwnd As Long
    hwnd = FindWindow("Notepad", vbNullString) ' find file
    SendMessage hwnd, &H10, 0, 0 ' kill filee
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
.
The following is designed for Sheet1. You can edit the macro for a different sheet and a different cell :

Paste in the Sheet 1 module :

Code:
Option Explicit


Sub worksheet_change(ByVal target As Range)
Set target = Range("A1")
    If target.Value = "Notepad" Then
        Command1_Click
    End If
    
    If target.Value = "notepad" Then
        Command1_Click
    End If
End Sub

Paste in a Routine Module :

Code:
Option Explicit


Sub Command1_Click()
    TerminateProcess ("notepad.exe")
End Sub
 
Private Sub TerminateProcess(app_exe As String)
    Dim Process As Object
    For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & app_exe & "'")
        Process.Terminate
    Next
End Sub
 
Upvote 0
There may be a situation where the notepad document is not saved and therefore it may not be suitable to treminate it by brute force.

The following API based routine gives you the possibility to skip the notepads that are not saved by leaving out the second optional argument or by setting it to False.
Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetShellWindow Lib "user32" () As LongPtr
    Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
    Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long
    Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
    Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
    Private Declare PtrSafe Function TerminateProcess Lib "kernel32" (ByVal hProcess As LongPtr, ByVal uExitCode As Long) As Long
    Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As LongPtr) As Long
#Else
    Private Declare Function GetShellWindow Lib "user32" () As Long
    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private 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
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
#End If

Private Const PROCESS_TERMINATE = &H1
Private Const GW_HWNDPREV = 3
Private Const WM_CLOSE = &H10

Sub KillAllByClassName(ByVal ClassName As String, Optional ByVal ForceKill As Boolean = False)
    #If VBA7 Then
        Dim hwnd As LongPtr, hProc As LongPtr
    #Else
        Dim hwnd As Long, hProc As Long
    #End If
    Dim ret As Long, sBuffer As String * 256
    Dim hPid As Long, lExitCode As Long

    hwnd = GetShellWindow
    Do While hwnd <> 0
        ret = GetClassName(hwnd, sBuffer, Len(sBuffer))
        If Left(sBuffer, ret) = ClassName Then
            If ForceKill Then
                Call GetWindowThreadProcessId(hwnd, hPid)
                hProc = OpenProcess(PROCESS_TERMINATE, 0, hPid)
                Call TerminateProcess(hProc, lExitCode)
                Call CloseHandle(hProc)
            Else
                Call PostMessage(hwnd, WM_CLOSE, 0, 0)
            End If
        End If
        hwnd = GetNextWindow(hwnd, GW_HWNDPREV)
    Loop
End Sub

Code usage:
Code:
Sub TEST()
    Call KillAllByClassName(ClassName:="Notepad", ForceKill:=False)
End Sub

If you want to force close all the notepads including the unsaved ones then set the ForceKill argument to True.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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