Excel VBA Sendkeys without delay

这一生的挚爱 提交于 2021-02-07 11:04:22

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/21387812/excel-vba-sendkeys-without-delay

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!