如何VBA等待窗口保存对话框和发送键



我正在创建一个宏文件,用于下载和保存SAP旧版本7.20中提取的数据,当出现保存对话框时,由于我的客户端SAP版本是旧版本7.20。现在我的解决方案是发送密钥,但问题是一些数据包含大量的密钥,发送密钥的时间不可靠。

如何等待保存对话框以及何时出现发送密钥。

Sub test()
waitTime (10000)
Call SendKeys("{Enter}", True)
End Sub
Function waitTime(ByVal miliseconds As Double)
Application.Wait (Now() + miliseconds / 24 / 60 / 60 / 1000)
End Function

假设您有一些类似的SAPGUI自动化代码

' This is the last line where you trigger some action within SAPGUI
.findById("wnd[1]/usr/....     
' Let's assume this line triggers the download and with it the Windows dialog box 
.findById("wnd[1]/tbar[0]/btn[0]").press

然后,您必须在触发窗口对话框男孩的行之前添加vba脚本的调用,即

' This is the last line where you trigger some action within SAPGUI
.findById("wnd[1]/usr/....  
dim SaveAs as string
dim xlFile as string
SaveAs ="Full Path to SaveAs.Vbs"
xlFile = "Full Path to the xls file"
Shell "wscript " & SaveAs & xlFile & " Save as"    
' Let's assume this line triggers the download and therefore the WIndows dialog box
.findById("wnd[1]/tbar[0]/btn[0]").press

SaveAs.vbs脚本可能看起来像

' WScript.Echo WScript.Arguments.Count
if Wscript.Arguments.count > 0 then 
' This first section deletes the file if it already exists, to avoid a prompt to overwrite.
set fs = CreateObject("Scripting.FileSystemObject")
if fs.fileExists(WScript.arguments(0)) then
Set myfile = fs.GetFile(WScript.arguments(0)) 
myfile.Delete
end if
'this loop runs until the Save As window finally appears
set Wshell = CreateObject("WScript.Shell")
Do 
' Argument 1 must be the  excat caption of the Save As dialogbox:
bWindowFound = Wshell.AppActivate(WScript.arguments(1))
WScript.Sleep 1000
Loop Until bWindowFound
Wshell.appActivate WScript.arguments(1)
Wshell.sendkeys "%n"       ' <= Keyboard short cut for Alt-n, you might need to change the n to your shortcut 
WScript.Sleep 400
Wshell.sendkeys WScript.arguments(0)
Wshell.sendkeys "%s"      ' <= Keyboard short cut for Alt-s, you might need to change the n to your shortcut 
WScript.Sleep 400
end if

最新更新