Sendkey两次取得成功?

问题描述 投票:8回答:2

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

UPDATE2

我尝试使用sleep()Application.wait()建议的@peh。我发现,一旦宏被初始化,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"

潘内尔

enter image description here

vba excel-vba sendkeys excel
2个回答
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

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

If GetHandleFromPartialCaption(lhWndP, "Your Dialogue Box Caption") = True Then
SendKeys(....

0
投票

我现在在我的linux盒子上,所以我不能修改它来测试,但你可能会试图用以下实用程序读取窗口的其他属性:

https://autohotkey.com/boards/viewtopic.php?t=28220

编辑:如果SendKeys绝对不起作用,并且你不想进入UI自动化路由,并且你不介意依赖,你可以从VBA安装AutoHotkey和脚本(例如使用Shell()命令)。在键盘宏自动化方面,AHK更加强大。

例如,如果您有一个唯一的类名,则可以使用FindWindowEx来获取窗口句柄:

模块范围〜

#If VBA7 Then
'32-bit declare
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
#Else
'64-bit declare
Private Declare PtrSafe Function FindWindowEx Lib "USER32" _
                              Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, _
                              ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
#End If

程序〜

Dim appcaption as String
appcaption = "Excel"

#If VBA7 Then
Dim parenthandle as Long, childhandle as Long
#Else
Dim parenthandle as LongPtr, childhandle as LongPtr
#End If
parenthandle = FindWindow(vbNullString, appcaption)

If parenthandle Then

    childhandle = GetWindow(parenthandle, GW_CHILD)1   
    Do Until Not childhandle
        childhandle = GetWindow(childhandle, GW_HWNDNEXT)
    Loop

End If

If childhandle Then
    '
End If

此代码仅是概念证明,例如,您可以打开多个Excel Windows。然而,它应该是一个很好的起点。

© www.soinside.com 2019 - 2024. All rights reserved.