' Fonction qui lance le PDF et renvoie le handle
Function OuvrirPDF_GetHandle() As LongPtr
Dim cheminPDF$, hwnd As LongPtr, sStr$, st$, tim#, a&
OuvrirPDF_GetHandle = 0 ' valeur par défaut
cheminPDF = Application.GetOpenFilename("Fichiers PDF (*.pdf), *.pdf", 1, "Ouvrir un fichier")
If cheminPDF = "Faux" Or Dir(cheminPDF) = "" Then
MsgBox "Fichier PDF introuvable.", vbExclamation
Exit Function
End If
' Lance le PDF avec l'application par défaut
'Shell "explorer """ & cheminPDF & """", vbNormalFocus
ShellExecute 0, "Open", cheminPDF, 0, 0, SW_SHOWNORMAL
' Recherche du handle
sStr = Space$(512)
st = String(256, vbNullChar)
tim = Timer
a = 0
Do While a < 10 And OuvrirPDF_GetHandle = 0
a = a + 1
hwnd = FindWindow(vbNullString, vbNullString)
Do While hwnd <> 0 And Timer - tim < 5
DoEvents
GetWindowText hwnd, sStr, 512
GetClassName hwnd, st, 256
If (sStr) Like "*" & Split(Mid(cheminPDF, InStrRev(cheminPDF, "\") + 1), ".")(0) & "*" Then
Debug.Print "titre : " & sStr
Debug.Print "classe : " & st
OuvrirPDF_GetHandle = hwnd
Exit Do
End If
hwnd = GetWindow(hwnd, 2)
Loop
If OuvrirPDF_GetHandle <> 0 Then Exit Do
Loop
End Function