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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public instance
Public Const GW_HWNDNEXT = 2
Sub macro()
Dim manavig
manavig = Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe https://chemin/nomdufichier.xls")
instance = Null
If Not WaitConnexion(5) Then
'apparition du pop-up d'identification en moins de 5s
'gestion du pop-up grâce à des SendKeys (pour entrée login et mot de passe)
End If
If Not WaitFichier(5) Then
'ouverture du pop-up demandant si on ouvre ou si on enregistre le fichier (on choisit d'ouvrir) le fichier en moins de 5s
'gestion du pop-up grâce à des SendKeys (pour seulement simuler la touche Entrée de manière à ouvrir le fichier)
Else
MsgBox "Le site ne s'est pas chargé suffisamment rapidement!", vbOKOnly + vbExclamation
Exit Sub
End If
If Not WaitFichier(60) Then
'fichier chargé en moins de 60s
SetForegroundWindow instance 'je passe le fichier au premier plan
ActiveWorkbook.Unprotect 'je tente de supprimer le mode protégé... mais Excel ne reconnait pas ActiveWorkbook
Dim Wb As Excel.Workbook
Dim Appli As Excel.Application
On Error Resume Next
Set Appli = GetObject(, "Excel.Application")
For Each Wb In Appli.Workbooks
If Left(Wb.Name, Len("nomdufichier")) = "nomdufichier" Then 'permet d'éviter les numéros d'instances du type "nomdufichier (2)", etc.
'... et il ne sait donc pas trouver le fichier quand il est en mode protégé...
End If
Next Wb
End If
End Sub
Public Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
'permet de trouver la fenêtre désirée
Dim lhWndP As Long
Dim sStr As String
GetHandleFromPartialCaption = False
lhWndP = FindWindow(vbNullString, vbNullString)
Do While lhWndP <> 0
sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
GetWindowText lhWndP, sStr, Len(sStr)
sStr = Left$(sStr, Len(sStr) - 1)
If InStr(1, sStr, sCaption) > 0 Then
GetHandleFromPartialCaption = True
lWnd = lhWndP
Exit Do
End If
lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
Loop
End Function
Public Function WaitConnexion(pTimeOut As Long) As Boolean
' Attend que le pop-up d'identification soit chargé
' pTimeOut est un time out en secondes (WaitFichier vaut True si Timeout)
Dim lhWndP As Long
Dim lTimer As Double
lTimer = Timer
Do
DoEvents
If GetHandleFromPartialCaption(lhWndP, "Authentification requise") = True Then Exit Do
If pTimeOut > 0 And Timer - lTimer > pTimeOut Then
WaitConnexion = True
Exit Do
End If
Loop
End Function
Public Function WaitFichier(pTimeOut As Long) As Boolean
' Attend que le fichier soit chargé
' pTimeOut est un time out en secondes (WaitFichier vaut True si Timeout)
Dim lhWndP As Long
Dim lTimer As Double
lTimer = Timer
Do
DoEvents
If GetHandleFromPartialCaption(lhWndP, "nomdufichier") = True Then instance = lhWndP: Exit Do 'on attribue la valeur du handle à la variable instance
If pTimeOut > 0 And Timer - lTimer > pTimeOut Then
WaitFichier = True
Exit Do
End If
Loop
End Function |
Partager