Search code examples
excelvbasap-gui

Stop transaction in SAP with VBA


I have a working VBA macro which enters SAP, starts a transaction and then extracts the data in spreadsheet.

But sometimes the calculation runs too long, or I just would like to stop it to intervene. There is a functionality on the toolbar at the top left corner, where the user can "stop transaction" manually.

Is there any SAP script code for the "stop transaction" button, so I can avoid the manual step?

SAP toolbar:

SAP toolbar

Solution

  • It is assumed that the VBA macro is running in the first session. If a second session is opened before starting the macro, it can be used to close the first session.

    for example:

    Set SapGuiAuto  = GetObject("SAPGUI")
    Set SAPapp = SapGuiAuto.GetScriptingEngine
    Set SAPconnection = SAPapp.Children(0)
    Set session    = SAPconnection.Children(1)
    
    session.findById("wnd[0]/tbar[0]/okcd").text = "/i1"
    session.findById("wnd[0]").sendVKey 0
    session.createSession
    
    Application.Wait (Now + TimeValue("0:00:05"))
    
    session.findById("wnd[0]/tbar[0]/okcd").text = "/i3"
    session.findById("wnd[0]").sendVKey 0
    session.createSession
    
    Application.Wait (Now + TimeValue("0:00:05"))
    

    Whether a "rollback" is carried out or not, would be to test.

    Regards, ScriptMan