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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| Option Explicit
Option Compare Binary
#If Win64 Then
' Pour le presse-papiers:
Private Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As LongPtr, ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function CloseClipboard Lib "User32" () As LongPtr
Private Declare PtrSafe Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
Private Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As Long
Private Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
Private Declare PtrSafe Function EmptyClipboard Lib "User32" () As LongPtr
Private Declare PtrSafe Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)
' Pour gérer les fenêtres actives:
Private Declare PtrSafe Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetWindow Lib "User32" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As LongPtr
Private Declare PtrSafe Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function SetFocus Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
#Else
' Pour le presse-papiers:
Private Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function CloseClipboard Lib "User32" () As Long
Private Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Private Declare Function EmptyClipboard Lib "User32" () As Long
Private Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
' Pour gérer les fenêtres actives:
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetWindow Lib "User32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function SetFocus Lib "User32" (ByVal hwnd As Long) As Long
#End If
Private Const CF_TEXT = 1
Private Const MAXSIZE = 65536
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
'------------------------------------------------------------------------------------------------------
Sub Ouvrir_PDF()
'------------------------------------------------------------------------------------------------------
Dim Hdc As LongPtr
Dim T As Double
' Ouvre le fichier PDF:
Shell "explorer.exe C:\Users\ott_l\Downloads\LeFichierPDF.pdf", vbMaximizedFocus
' Boucle tant que le Handle de Adobe n'est pas trouvé:
T = Timer
Do
Hdc = Hdc_Trouver("*Adobe*")
Loop While Hdc = 0 And T + 10 > Timer
' Place le focus sur l'application et envoie Control L:
If Hdc <> 0 Then
Hdc_EnvoyerTouches Hdc, vbKeyControl, vbKeyL
End If
End Sub
'------------------------------------------------------------------------------------------------------
Public Function Hdc_Trouver(StrFenetre As String) As LongPtr
'------------------------------------------------------------------------------------------------------
' Retourne le Handle de la fenêtre passé en argument. Utilise l'opérateur Like donc accepte * et ?
' et autres, voir l'aide.
' Retourne 0 si la fenêtre n'est pas trouvée.
'------------------------------------------------------------------------------------------------------
Dim Ret As LongPtr
Dim MyStr As String
' Boucle sur les fenêtres actives:
Ret = FindWindow(ByVal 0&, ByVal 0&)
Do While Ret <> 0
' Cherche le nom de la fenêtre:
MyStr = String(100, Chr$(0))
GetWindowText Ret, MyStr, Len(MyStr)
' Si c'est la fenêtre recherchée alors renvoie l'Hdc:
If Left(MyStr, InStr(1, MyStr, Chr(0)) - 1) Like StrFenetre Then
Hdc_Trouver = Ret
Exit Do
End If
' Cherche la fenêtre suivante:
Ret = GetWindow(Ret, 2)
Loop
End Function
'------------------------------------------------------------------------------------------------------
Public Sub Hdc_EnvoyerTouches(hWndApp As Variant, ParamArray Combinaison() As Variant)
'------------------------------------------------------------------------------------------------------
' Envoie des touches à une application.
' hWndApp : est soit le numéro Hdc de la fenêtre (ou 0 si fenêtre active), soit son nom.
' Combinaison : touche(s) a envoyer. Ce peut être une chaîne ou une variable vbKey.
'------------------------------------------------------------------------------------------------------
' Exemples : Hdc_EnvoyerTouches Hdc, "Bonjour"
' Hdc_EnvoyerTouches Hdc, vbKeyMultiply
'------------------------------------------------------------------------------------------------------
' Astuce : l'impression écran est impossible avec Sendkeys, utilisez Hdc_EnvoyerTouches 0, vbKeySnapshot
' ou pour n'avoir que le forumaire actif: Hdc_EnvoyerTouches 0, vbKeyMenu, vbKeySnapshot
' Exmple pour la calculatrice (si déjà ouverte) : Hdc_EnvoyerTouches "*Calculatrice*", vbKeyMenu, vbKeySnapshot
' puis pour coller dans Excel : Sheets("Feuil1").Paste Range("A1")
'------------------------------------------------------------------------------------------------------
Dim i As Integer, j As Integer, s As String, Etat As Boolean, Maj As Boolean
Dim Hdc As LongPtr
If IsNumeric(hWndApp) = True Then
Hdc = hWndApp
Else
Hdc = Hdc_Trouver(CStr(hWndApp))
End If
' Vide la presse-papiers:
ClipBoard_Clear
' Place le focus sur la fenêtre demandée (si son numéro est passé <> 0):
If Hdc <> 0 Then
SetForegroundWindow Hdc
SetFocus Hdc
End If
' Si une chaîne de carractères est passée en argument:
If VarType(Combinaison(0)) <> vbInteger Then
' L'envoie dans le presse-papiers:
If ClipBoard_SetData(CStr(Combinaison(0))) = True Then
' Si cela réussi alors colle avec Ctrl+V:
keybd_event vbKeyControl, 0, 0, 0
keybd_event vbKeyV, 0, 0, 0
keybd_event vbKeyControl, 0, 2, 0
keybd_event vbKeyV, 0, 2, 0
End If
' Si c'est une combinaison numérique qui est passée en argument:
Else
' Active:
For i = LBound(Combinaison()) To UBound(Combinaison())
keybd_event Combinaison(i), 0, 0, 0
Next i
' Relache:
For i = LBound(Combinaison()) To UBound(Combinaison())
keybd_event Combinaison(i), 0, 2, 0
Next i
End If
DoEvents
End Sub
'---------------------------------------------------------------------------------------
Public Function ClipBoard_Clear() As Boolean
'---------------------------------------------------------------------------------------
If OpenClipboard(0) = 0 Then Exit Function ' Could not open the Clipboard.
EmptyClipboard
If CloseClipboard() <> 0 Then ClipBoard_Clear = True
End Function
'---------------------------------------------------------------------------------------
Private Function ClipBoard_SetData(MyString As String) As Boolean
'---------------------------------------------------------------------------------------
' Envoie une chaîne de caractères dans le presse-papiers.
' MyString : Chaîne à envoyer
'---------------------------------------------------------------------------------------
' Sources : 32-bit code by Microsoft: http://msdn.microsoft.com/en-us/library/office/ff192913.aspx
'---------------------------------------------------------------------------------------
Dim hGlobalMemory As LongPtr, lpGlobalMemory As LongPtr
Dim hClipMemory As LongPtr
' Gestion de la taille maximale du message a envoyer au clavier:
If Len(MyString) >= MAXSIZE Then ' "Chaîne trop grande"
Exit Function
End If
' Gestion des chaînes vides:
If MyString = "" Then Exit Function
' Allocate moveable global memory.
hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)
' Lock the block to get a far pointer to this memory.
lpGlobalMemory = GlobalLock(hGlobalMemory)
' Copy the string to this global memory.
lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)
' Unlock the memory.
If GlobalUnlock(hGlobalMemory) <> 0 Then '"Could not unlock memory location. Copy aborted."
GoTo OutOfHere
End If
' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then ' "Could not open the Clipboard. Copy aborted."
Exit Function
End If
' Clear the Clipboard.
EmptyClipboard
' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
OutOfHere:
If CloseClipboard() <> 0 Then ClipBoard_SetData = True
End Function
'---------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------- |
Partager