【问题标题】:How to VBA wait for windows save dialogbox and sendkeys如何VBA等待Windows保存对话框和sendkeys
【发布时间】:2020-03-05 19:52:30
【问题描述】:

我正在创建一个宏文件,用于从 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

【问题讨论】:

  • 您可以创建一个 vbs 脚本,它至少在我过去使用我的 SAPGUI 脚本时,当本机 Windows 对话框出现时。但是对于7.30 PL 8,这不再是必要的了。
  • 你能帮我看看你过去是怎么做到的吗?
  • 我假设您有 SAPGUI 的自动化代码,请张贴并指出之后会出现调用 windows 对话框(通常是另存为)。

标签: excel vba excel-2007 sap-gui


【解决方案1】:

假设您有一些这样的 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

【讨论】:

  • Wscript 需要添加到引用上吗?参考哪一个? :) 非常感谢您提供的好样品!
  • 嗨 storax,你能帮忙创建 saveas.vbs 吗,这是完整的声明吗?很抱歉,我是 vbscripting 的新手 :(
  • Wscript 不需要添加到引用中。我已经发布了文件 saveas.vbs 的内容。 Shell "wscript " &amp; SaveAs &amp; xlFile &amp; " Save as" 行只调用您系统上的wscript,并使用文件SaveAs 和参数xlfile(即保存SAP 下载的文件)和" Save As"。这应该与出现的 Windows 对话框的标题相匹配。
  • 嗨,Storax!我正在尝试执行脚本,但我从 Windows 脚本主机收到此消息:“没有文件扩展名 '.xls' 的脚本引擎”你知道如何解决这个问题吗?
  • vbs 脚本的扩展名必须是 VBS。如果这没有帮助,请创建一个新问题,其中包含您尝试过的所有详细信息、无效的内容以及收到的错误消息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
相关资源
最近更新 更多