Как в VBA дождаться появления окна сохранения диалогового окна и отправки ключей - PullRequest
0 голосов
/ 09 ноября 2019

Я создаю файл макроса, который загружает и сохраняет извлеченные данные из 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

1 Ответ

0 голосов
/ 09 ноября 2019

Предположим, у вас есть некоторый код автоматизации 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 непосредственно перед строкой, в которой запускается диалоговое окно Windows, то есть

 ' 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
...