如何使用VBA等待Windows保存对话框和sendkeys

问题描述 投票:0回答:1

我正在创建一个宏文件,该文件下载并保存从SAP旧版7.20提取的数据,当出现保存对话框时,由于我的客户端SAP旧版7.20,未检测到Windows对话框。现在我的解决方法是sendkeys,但问题是某些数据包含大量数据,导致发送密钥的时间变得不可靠。

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

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
excel vba excel-2007 sap-gui
1个回答
0
投票

假设您有类似的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
© www.soinside.com 2019 - 2024. All rights reserved.