Function ApplicationIsRunning(ApplicationClassName As String) As Boolean
' returns True if the application is running
' example: If Not ApplicationIsRunning('Outlook.Application') Then Exit Sub
Dim AnyApp As Object
On Error Resume Next
Set AnyApp = GetObject(, ApplicationClassName)
ApplicationIsRunning = Not AnyApp Is Nothing
Set AnyApp = Nothing
On Error GoTo 0
End Function
Public Declare Function FindWindow Lib 'user32' Alias 'FindWindowA' _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Function IsAppRunning(sProg As String) as boolean
Dim HWnd As Long
IsAppRunning = False
HWnd = FindWindow(vbNullString, sProg)
If HWnd > 0 Then
IsAppRunning = True
End If
End Sub
Declare Function FindWindow Lib 'user32' Alias 'FindWindowA' _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function EnumWindows Lib 'user32' (ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib 'user32' Alias 'GetWindowTextA' (ByVal hwnd As Long, _
ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib 'user32' Alias 'GetWindowTextLengthA' (ByVal hwnd As Long) As Long
Declare Function GetClassName Lib 'user32' Alias 'GetClassNameA' _
(ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
'Lance la procédure d'énumération
Sub Test()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
'Enumère les fenêtres dans la session
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
Debug.Print Str$(hwnd) + ' / ' + sSave + ' / ' + sClass(hwnd)
'continue enumeration
EnumWindowsProc = True
End Function
'Donne le nom de la class du handle de la fenêtre
Function sClass(x As Long)
Dim className As String
Dim retValue As Long
className = String$(256, ' ')
retValue = GetClassName(x, className, 255)
sClass = Left$(className, InStr(className, vbNullChar) - 1)
End Function
'Test si un programme est lancé ou non
Function IsAppRunning(x As String) As Boolean
Dim hwnd As Long
IsAppRunning = False
hwnd = FindWindow(x, vbNullString)
If hwnd > 0 Then
IsAppRunning = True
End If
End Function