Excel VBA Sendkeys without delay

I'm trying to copy some cells from excel to another Windows application so fast as possible using Sendkeys. If I don't use "wait", Sendkeys fails. And if use it, Sendkeys run slowly and I need it in full speed, realtime if possible. Anyone can help me? Sorry for my English, I'm a brazilian student. Thank you

Public Const MOUSEEVENTF_RIGHTUP As Long = &H10



Private Sub SingleClick()
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub DoubleClick()
  'Simulate a double click as a quick series of two clicks
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub RightClick()
  'Simulate a right click
  mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_RIGHTTUP, 0, 0, 0, 0
End Sub


Sub Botão1_Clique()

'copy b2 and past in a specific area (another windows application)
Range("B2").Copy
SetCursorPos 765, 70
SingleClick
Application.SendKeys "^v"

Application.Wait (Now + 0.000007) ' It is the faster I can use but it's not enough

Range("B3").Copy
SetCursorPos 765, 80
SingleClick
Application.SendKeys "^v"

Application.Wait (Now + 0.000007)

Range("B4").Copy
SetCursorPos 765, 90
SingleClick
Application.SendKeys "^v"


End Sub

Answers


While this likely isn't the best way to perform real-time trading, I think you can modify your SendKey statement to include the wait. I believe this Application.SendKeys "^v", True will force your code to pause for as long as it takes the command to execute, which might be faster than what you've coded.


Don't use SendKeys. They are highly unreliable. Use the FindWindow/FindWindowEx/SendMessage API

See THIS example where I have demonstrated on how to paste to a 3rd Party Application.

Based on that here is a simple example on how to paste from Excel to a 3rd party App.

Sample Code (UNTESTED)

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Const WM_PASTE As Long = &H302

Sub Botão1_Clique()
    Dim Ret
    Dim i As Long

    '~~> Change "BLAHBLAH" to the caption of the 3rd party app.
    Ret = FindWindow(vbNullString, "BLAHBLAH")

    If Ret <> 0 Then
        '~~> Change "BLAHBLAH" to the type of textbox
        '~~> Assuming that you want to paste to textbox else change it
        Ret = FindWindowEx(Ret, ByVal 0&, "BLAHBLAH", vbNullString)

        If Ret <> 0 Then
            '~~> Looping through only 5 cells
            '~~> Change as applicable
            For i = 1 To 5
                ThisWorkbook.Sheets("Sheet1").Range("B" & i).Copy

                '~~> Paste
                SendMessage Ret, WM_PASTE, 0, ByVal 0

                DoEvents
            Next i
        Else
            Debug.Print "TextBox in 3rd Party Application Not Found"
        End If
    Else
        Debug.Print "3rd party Application Not Found"
        Exit Sub
    End If
End Sub

Need Your Help

Keil uVision 5: How can I copy array data out into a file on my PC?

debugging copy-paste keil

I've just now started to learn programming a Cortex-m0 based microcontroller which is on a EVK connected to my computer by USB. The expected IDE/toolchain to be used for this EVK is uVision 4/5 and...

JavaScript DEFLATE Implementation

javascript algorithm deflate

Are there any open source DEFLATE encoder implementations for JavaScript? I need it to generate a binary format on the client-side that requires DEFLATE.