This is my VBA code:
Sub logowanie() UserForm1.Show vSAP = Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus) Call Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus) Set WSHShell = CreateObject("WScript.Shell") Do Until WSHShell.AppActivate("SAP Logon") Application.Wait Now + TimeValue("0:00:01") Loop Set SapGui = GetObject("SAPGUI") Set Appl = SapGui.GetScriptingEngine Application.Wait Now + TimeValue("0:00:01") Set connection = Appl.Openconnection("xxxxxxxxxx", True) Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys UserForm1.TextBox1.Value WSHShell.SendKeys "{TAB}" WSHShell.SendKeys UserForm1.TextBox2.Value WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:01") WSHShell.SendKeys "y_ecd_96000032" WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:01") WSHShell.SendKeys "{DOWN}" WSHShell.SendKeys "{DOWN}" WSHShell.SendKeys "{DOWN}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "22:00:00" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "*" WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "DC15" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "{DOWN}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "^{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "U:\[...]\a.txt" WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:03") WSHShell.SendKeys "{F8}" Application.Wait Now + TimeValue("0:00:03") WSHShell.SendKeys "{F8}" Application.Wait Now + TimeValue("0:00:03") WSHShell.SendKeys "+{F4}" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "U:\[...]\SRET.xlsx" WSHShell.SendKeys "{ENTER}" 'Application.Wait Now + TimeValue("0:00:03") WSHShell.SendKeys "{LEFT}" WSHShell.SendKeys "{ENTER}" Application.Wait Now + TimeValue("0:00:04") AppActivate (vSAP) Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "%{F4}" Application.Wait Now + TimeValue("0:00:02") WSHShell.SendKeys "{TAB}" WSHShell.SendKeys "{ENTER}" End Sub
I need Application.Wait Now + [some time value] between commands. But sometimes it doesn't work, when SAP working too slow. How to do it? I need some Loop with Application.Wait when each dialog windows showing up.