我正在使用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

更新

还尝试用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

更新2

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

还尝试添加DoEventssendkey1完美。但是,只有在单击取消按钮后,才会启动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"

面板

vba - Sendkey基于成功两次?-LMLPHP

最佳答案

如果对话框每次都是相同的,或者在标题中包含一致的文本字符串,则可以使用此标题来检测该对话框何时出现,使用此功能在带有计时器的循环中使用计时器搜索合理数量的对话框的时间:

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

sCaption是对话框的名称。然后在您的代码主体中使用:
If GetHandleFromPartialCaption(lhWndP, "Your Dialogue Box Caption") = True Then
SendKeys(....

关于vba - Sendkey基于成功两次?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47828009/

10-10 18:48