我使用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
也会启动。
还尝试添加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"
潘内尔
答案 0 :(得分: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(....
答案 1 :(得分:0)
我现在在我的linux盒子上,所以我不能修改它来测试,但你可能会试图用以下实用程序读取窗口的其他属性:
https://autohotkey.com/boards/viewtopic.php?t=28220
编辑:如果
SendKeys
绝对不起作用,并且你不想进入UI自动化路线,和你不介意依赖,你可以安装AutoHotkey和来自VBA的脚本(例如使用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窗口。不过,它应该是一个很好的起点。