Code in the module:
Option Explicit
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function EnumWindows Lib "user32.dll" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Target As String
Private IsOpen As Boolean
Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long
Dim hWndREC As Long
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)
If InStr(1, title, Target, vbTextCompare) Then
IsOpen = True
End If
EnumCallback = 1
End Function
Public Function WindowOpen(app_name As String) As Boolean
'app_name is the name of the application when tracked on Windows Task Manager
IsOpen = False
Target = app_name
EnumWindows AddressOf EnumCallback, 0
If IsOpen Then WindowOpen = True
End Function
To check whether a program is running, e.g. Notepad:
If WindowOpen("Notepad") Then
MsgBox "Notepad is opened"
Else
MsgBox "Notepad is not opened"
End If
0 comments:
Post a Comment