问题
I'm creating a macro file that download and save extracted data from SAP old version 7.20 , when the save dialogbox is appear , the windows dialogbox is not detected since my client SAP version is old 7.20. now my resolution on this is the sendkeys but the problem is some of the data is containing large amount that getting unreliable time to send the keys.
How to wait for the save dialogbox and when appear 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
回答1:
Let's assume you have some SAPGUI automation code like that
' 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
Then you have to add the call of the vba script just before the line where the windows dialog boy is triggered, i.e.
' 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
The SaveAs.vbs script could look like that
' 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
来源:https://stackoverflow.com/questions/58779126/how-to-vba-wait-for-windows-save-dialogbox-and-sendkeys