excelvbasap-gui

How to create a pause in SAP GUI?


After I connect to SAP GUI via Excel, I'm having problems acquiring data from SAP GUI.

The macro is too fast. If it is paused, the logic works, but without pausing, it doesn't.

I would like to include time when running in SAP GUI.

Sub Sap()

Dim Application, SapGuiAuto, Connection, session, WScrip

If Not IsObject(Application) Then
    Set SapGuiAuto = GetObject("SAPGUI")
    Set Application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
    Set Connection = Application.Children(0)
End If
If Not IsObject(session) Then
    Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
    WScript.ConnectObject session, "on"
    WScript.ConnectObject Application, "on"
End If
'Login
session.findbyid("wnd[0]").maximize
session.findbyid("wnd[0]/usr/txtRSYST-BNAME").Text = "mylogin"
session.findbyid("wnd[0]/usr/pwdRSYST-BCODE").Text = "password"
session.findbyid("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
session.findbyid("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
session.findbyid("wnd[0]").sendVKey 0
'MM02
session.findbyid("wnd[0]/tbar[0]/okcd").Text = "mm02"
session.findbyid("wnd[0]").sendVKey 0
Application.Wait Now + TimeValue("00:00:03") '——->>>>> BUT NOT WORK THIS FUNCTION

'continueing…

End Sub

Solution

  • There is also another way to generate a wait in VBA.

    for example:

    Sub Sap()
    
    Dim Application, SapGuiAuto, Connection, session, WScript
    set wshell = CreateObject("Wscript.Shell")
    ...
    wshell.run "c:\tmp\sleep_3000.vbs",1,true
    'continueing…
    End Sub
    

    sleep_3000.vbs in c:\tmp

    wscript.sleep 3000
    

    Regards, ScriptMan