Sendkey两次取得成功?

aaa*_*aaa 8 excel vba excel-vba sendkeys

sendkey用来访问Power Query并连接到SharePoint文件夹.一切顺利,直到Power Query Data Preview对话框出现.

sendkey对话框出现后如何允许继续?我正在使用按钮启动宏并使用Excel 2016.

Option Explicit
Sub Button1_Click()

    Dim spPath As String
    Dim strkeys As String

    spPath = "" 'SharePoint Link

    strkeys = "%APNFO" & spPath & "{Enter}{TAB 4}{Enter}" 
    'stops at first{Enter}, {TAB 4}{Enter} for EDIT
    Call SendKeys(strkeys)

End Sub
Run Code Online (Sandbox Code Playgroud)

更新

也尝试了sendkey两次True但结果相同,停止在对话框中.

Option Explicit
Sub Button1_Click()

    Dim spPath As String
    Dim strkeys As String
    Dim strkeys2 As String

    spPath = ""

    strkeys = "%APNFO" & spPath & "{Enter}"
    strkeys2 = "{TAB 4}{Enter}"
    Call SendKeys(Trim(strkeys), True)
    Call SendKeys(Trim(strkeys2), True)
    Debug.Print strkeys2

End Sub
Run Code Online (Sandbox Code Playgroud)

UPDATE2

我尝试了@peh的建议,使用sleep()Application.wait().我发现一旦宏被初始化,sendkey1启动和停止了Application.wait().只有在等待时间结束后sendkey1才会被处理.一旦sendkey1开始,sendkey2也开始了.

也尝试添加DoEvents,sendkey1工作完美.但是只有在点击后的取消按钮,Application.wait()并且sendkey2将开始.

Call SendKeys(Trim(strkeys))
Debug.Print Now & "Send Key 1"
'Do Events
Application.wait (Now + TimeValue("0:00:10"))
Call SendKeys(Trim(strkeys2), True)
Debug.Print Now & "Send Key 2"
Run Code Online (Sandbox Code Playgroud)

潘内尔

在此输入图像描述

Pet*_*hor 6

如果对话框每次都相同,或者在标题中包含一致的文本字符串,您可以使用它的标题来检测何时使用此函数在循环中使用定时器搜索合理数量的对话框的时间:

Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean

Dim lhWndP As Long
Dim sStr As String
GetHandleFromPartialCaption = False
lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
Do While lhWndP <> 0
    sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    GetWindowText lhWndP, sStr, Len(sStr)
    sStr = Left$(sStr, Len(sStr) - 1)
    If InStr(1, sStr, sCaption) > 0 Then
        GetHandleFromPartialCaption = True
        lWnd = lhWndP
        Exit Do
    End If
    lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
Loop
End Function
Run Code Online (Sandbox Code Playgroud)

其中sCaption是对话框的名称.然后在你的主体代码中使用:

If GetHandleFromPartialCaption(lhWndP, "Your Dialogue Box Caption") = True Then
SendKeys(....
Run Code Online (Sandbox Code Playgroud)