VBA - Close notepad
Sub test() Dim StrPath As String StrPath = "C:\New folder\Test.txt" ' Please type FullName Call Close_Notepad_ByName(StrPath) End Sub Public Sub Close_Notepad_ByName(NtpPath As String) Dim oServ As Object Dim cProc As Object Dim oProc As Object StrProcessName = "Notepad.exe" Set oServ = GetObject("winmgmts:") Set cProc = oServ.ExecQuery("select * from win32_process") For Each oProc In cProc If InStr(1, oProc.Name, StrProcessName, vbTextCompare) <> 0 Then' check if Notepad If InStr(1, oProc.CommandLine, NtpPath, vbTextCompare) <> 0 Then ' check Path oProc.Terminate End If End If Next Set oServ = Nothing Set cProc = Nothing End Sub
Source: social.msdn.microsoft.com