如何 VBA 等待 windows 保存对话框和发送密钥

How to VBA wait for windows save dialogbox and sendkeys

我正在创建一个从 SAP 旧版本 7.20 下载并保存提取数据的宏文件,当出现保存对话框时,未检测到 windows 对话框,因为我的客户端 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

然后你必须在 windows 对话框男孩被触发的行之前添加 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