1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| Option Explicit
#If VBA7 Then
Declare PtrSafe Function GetForegroundWindow Lib "User32" () As Long
Declare PtrSafe Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare PtrSafe Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
#Else
Declare Function GetForegroundWindow Lib "User32" () As Long
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
#End If
Function GetForGroundWindowTitle() As String
Dim fgwhwnd As Long
Dim sTitle As String
fgwhwnd = GetForegroundWindow
'on détermine la longueur de la chaine du titre de fenêtre
sTitle = Space$(GetWindowTextLength(fgwhwnd) + 1)
'on récupère le titre de la fenêtre
GetWindowText fgwhwnd, sTitle, Len(sTitle)
'Supprime le dernier Chr$(0)
sTitle = Left$(sTitle, Len(sTitle) - 1)
GetForGroundWindowTitle = sTitle
End Function
Sub Tempo(ByVal TempPause As Single) 'utile uniquement pour la démo ci-dessous
Dim start As Single
start = Timer
Do While Timer < start + TempPause
DoEvents ' Donne le contrôle à d'autres processus.
Loop
End Sub
Sub demo()
Dim NomFenetreActive As String
Tempo 3 'une petite tempo de 3" pour avoir le temps d'ouvrir une fenetre externe à excel
NomFenetreActive = GetForGroundWindowTitle()
MsgBox "Vous avez ouvert la fenêtre : " & NomFenetreActive
End Sub |
Partager