Une macro VBA qui cherche si une page Web est ouverte
Comment savoir si une page Web est ouverte ?
Pas de solution simple.
Une méthode un peu tordue en utilisant le titre de la page, auquel on peut accéder grace aux fonctions API :
Declare Function cherche_fenetre Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Declare Function get_titre Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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
Dim titre As String
Dim classe As String
Sub cherche_page_web(nomfen)
poign = cherche_fenetre(0, 0)
Do While poign <> 0
titre = String(100, Chr$(0))
get_titre poign, titre, 100
titre = Left$(titre, InStr(titre, Chr$(0)) - 1)
classe = String(100, Chr$(0))
GetClassName poign, classe, 100
classe = Left$(classe, InStr(classe, Chr$(0)) - 1)
If classe = "IEFrame" And InStr(LCase(titre), LCase(nomfen)) > 0 Then fenouv = True
poign = GetWindow(poign, 2)
Loop
If fenouv Then
MsgBox nomfen & " est ouvert"
Else
MsgBox nomfen & " n'est pas ouvert"
End If
End Sub
La macro balaie avec GetWindow toutes les fenêtres ouvertes, en détermine la classe avec GetClassNameA, puis, dans les fenêtres de classe IEFrame, recherche celles dont le nom (GetWindowTextA) correspond à la page cherchée.
[attention, la macro recherche une page ouverte dans internet explorer. Pour Firefox, remplacer "IEFrame" par "MozillaWindowClass".]
Pour savoir si le site JACXL est ouvert, il suffit d'écrire :
Sub test()
cherche_page_web ("Formation Excel")
End Sub
Formation Excel est le titre de la page principale du site (titre défini dans la page html par la balise <TITLE>).