Public Declare Function GetWindowText _
Lib "user32" _
Alias "GetWindowTextA" ( _
ByVal hWnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Public Declare Function EnumWindows _
Lib "user32" ( _
ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
Dim Ouvert As String
Dim NomProg As String
'proc de test, à adapter :
Sub Test()
Dim lResult As Long
NomProg = "Word" 'cherche si Word est ouvert
lResult = EnumWindows(AddressOf ProgrammeOuvert, 0&)
MsgBox Ouvert
End Sub
'cherche dans le titre des fenêtres le nom du programme
Public Function ProgrammeOuvert(ByVal hWnd As Long, _
ByVal lgParam As Long) As Long
Dim Buffer As String
Dim Result As Long
Buffer = Space(255)
Result = GetWindowText(hWnd, Buffer, 255)
If Left(Buffer, 1) <> Chr(0) Then
'non sensible à la casse (UCase)
If InStr(UCase(Trim(Buffer)), UCase(NomProg)) <> 0 Then
Ouvert = NomProg & " est ouvert !"
Exit Function
Else
Ouvert = NomProg & " est fermé !"
End If
End If
ProgrammeOuvert = 1
End Function