Sub a()
[A1].Value = "aaa"
[A1].Select
CreateObject("wscript.shell").SendKeys "bbb{ENTER}"
[B1].Value = [A1].Value
End Sub
Sub a()
[A1].Value = "aaa"
[A1].Select
SendKeys "bbb{ENTER}", True
[B1].Value = [A1].Value
End Sub
Function Xsendkeys(key, sens)
ExecuteExcel4Macro ("CALL(""user32"",""keybd_event"",""JJJJJ""," & key & ", " & 1 & ", " & sens & ", " & 0 & ")") 'api SetWindowLongA
End Function
Sub test()
Xsendkeys 17, 0 'on apui sur la touche CTRL
Xsendkeys 40, 0 ' on apui sur la touche bas
Xsendkeys 40, &H2 'on relache la touche bas
Xsendkeys 17, &H2 'on relache la touche CTRL
End Sub
' Pour d'autres touches...
' a à z 65 à 90
' Home 36
' End 35
' Flêche vers le haut 38
' Flêche vers le bas 40
' Flêche vers la gauche 37
' Flêche vers la droite 39
' Echap 27
' Impr écran 44 (= vbKeySnapshot)
' Page haut 33
' Page bas 34
' Insert 45
' F1 à F12 112 à 123
' Barre d'espace 32
' Ctrl 17
' Alt 18
' Maj 16
' Verr Num 144
' Arrêt défil 145
' Tab 9
' Shift 16
#If VBA7 Then
Private Declare PtrSafe Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer
#Else
Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer
#End If
Sub SendKeysWait(Keys As String)
Dim NumLock As Boolean
NumLock = IsNumLock
SendKeys Keys, True
If NumLock <> IsNumLock Then SendKeys "{NUMLOCK}", True
End Sub
Private Function IsNumLock() As Boolean
Const VK_NUMLOCK = &H90 ' ou 144
If GetKeyState(VK_NUMLOCK) Then IsNumLock = True
End Function
GetKeyState(VK_NUMLOCK)
ExecuteExcel4Macro("CALL(""user32"", ""GetKeyState"", ""JJ"", 144)")
Salut à tous,Pour un SendKeys Wait, il faut en passer par là:
VB:#If VBA7 Then Private Declare PtrSafe Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer #Else Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Long) As Integer #End If Sub SendKeysWait(Keys As String) Dim NumLock As Boolean NumLock = IsNumLock SendKeys Keys, True If NumLock <> IsNumLock Then SendKeys "{NUMLOCK}", True End Sub Private Function IsNumLock() As Boolean Const VK_NUMLOCK = &H90 ' ou 144 If GetKeyState(VK_NUMLOCK) Then IsNumLock = True End Function
@patricktoulon doit bien avoir un ExecuteMacro4Excel pour le GetKeyState.
Méthode [argument facultatif] | Impact possible sur NUMLOCK | Nativement synchrone (*) | |
1 | Sendkeys Keys [, Wait:=True | False] | Oui | Wait:=False -> Non Wait:=True -> Oui |
2 | Application.SendKeys Keys [, Wait:=True | False] | Oui | Wait:=False -> Non Wait:=True -> Non |
3 | CreateObject("wscript.shell").SendKeys Keys [, Wait:=True | False] | Non | Wait:=False -> Non Wait:=True -> Non |
Option Explicit
'https://answers.microsoft.com/en-us/msoffice/forum/all/checking-for-numlock-status-fixing-numlock-status/1f85d47b-c368-4de8-becd-039f947a50ba?auth=1
'https://stackoverflow.com/questions/42440776/turning-numlock-on-at-the-end-of-a-macro-run
#If VBA7 Then
Private Declare PtrSafe Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As LongPtr, ByVal dwExtraInfo As LongPtr)
Private Declare PtrSafe Function GetKeyboardState Lib "user32.dll" (ByVal lpKeyState As LongPtr) As Boolean
#Else
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32.dll" (ByVal lpKeyState As Long) As Boolean
#End If
Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
Private Const KEYEVENTF_KEYUP As Long = &H2
Private Const VK_NUMLOCK As Byte = &H90
Private Const NumLockScanCode As Byte = &H45
'------------------------------------
'Set NUMLOCK à la valeur du paramètre
'------------------------------------
Private Sub ToggleNumlock(Enabled As Boolean)
Dim KeyState(255) As Byte
'Test current keyboard state.
GetKeyboardState (VarPtr(KeyState(0)))
If (Not CBool(KeyState(VK_NUMLOCK)) And Enabled) Or (CBool(KeyState(VK_NUMLOCK)) And Not Enabled) Then
'Send a keydown
keybd_event VK_NUMLOCK, NumLockScanCode, KEYEVENTF_EXTENDEDKEY, 0&
'Send a keyup
keybd_event VK_NUMLOCK, NumLockScanCode, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0&
End If
End Sub
'--------------------------
'Retourne l'état du NUMLOCK
'--------------------------
Private Function NumLockState() As Boolean
Dim KeyState(255) As Byte
GetKeyboardState (VarPtr(KeyState(0)))
NumLockState = CBool(KeyState(VK_NUMLOCK))
End Function
'--------------------------
'Envoi de touches avec Wait
'--------------------------
Sub SendKeysWait(Keys As String)
Dim NumLock As Boolean
NumLock = NumLockState
SendKeys Keys, Wait:=True
Call ToggleNumlock(NumLock)
End Sub
Sub a()
Dim i As Integer
[A1].Value = "xyz"
[A2].ClearContents
[A1].Select
'Envoi de 2600 caractères pour valoriser [A1]
For i = 1 To 26
CreateObject("wscript.shell").SendKeys String(100, Chr(96 + i))
Next i
CreateObject("wscript.shell").SendKeys "{ENTER}"
DoEvents 'Laisse le temps au système d'envoyer tous les Sendkeys avant de reprendre le code VBA
[A2].Value = [A1].Value '[A1] a bien reçu tous les caractères et donc [A2] peut les récupérer
'Sans le DoEvents, [A2] aurait la valeur précédente de [A1], c'est à dire "xyz"
[A3].FormulaLocal = "=NBCAR(A2) & "" caractères en [A2]"""
End Sub
cela m' arrive aussi ,en effet , faudrait se rapprocher de David pour en connaitre la raisonTa méthode quoique plus longue en lignes de code fonctionne parfaitement.
Par contre, je ne suis plus notifié quand tu réagis aux post auxquels je participe ou crée !?
Function Xsendkeys(key, sens)
ExecuteExcel4Macro ("CALL(""user32"",""keybd_event"",""JJJJJ""," & key & ", " & 1 & ", " & sens & ", " & 0 & ")") 'api SetWindowLongA
DoEvents
End Function
Sub test2()
Xsendkeys 65, 0 'on apui sur la touche a
Xsendkeys 65, &H2 'on relache la touche a
Xsendkeys 66, 0 ' on apui sur la touche b
Xsendkeys 66, &H2 'on relache la touche b
Xsendkeys 67, 0 ' on apui sur la touche c
Xsendkeys 67, &H2 'on relache la touche c
Xsendkeys 13, 0 ' on apui sur la touche enter
Xsendkeys 13, &H2 'on relache la touche enter
Xsendkeys 38, 0 ' on apui sur la touche up
Xsendkeys 38, &H2 'on relache la touche up
End Sub