"-Begin-----------------------------------------------------------------
"-
"- Hint: It is necessary to disable the security rule for SAPROTWrapper
"-       in the SAP Logon, otherwise it is not allowed to create the
"-       object SapROTWr.SapROTWrapper.
"-
"- Hint: You can not use the same session to execute this program
"-       and to record it via SAP GUI Scripting. Run this program e.g.
"-       in session 0 and record session 1.
"-
"-----------------------------------------------------------------------
Report zGUIScripting.

  Include OLE2INCL.

  Data:
    SAPROT Type OLE2_OBJECT,
    ROTEntry Type OLE2_OBJECT,
    application Type OLE2_OBJECT,
    connections Type OLE2_OBJECT,
    connection Type OLE2_OBJECT,
    sessions Type OLE2_OBJECT,
    session Type OLE2_OBJECT.

  "-Main--------------------------------------------------------------
  PerForm StartRecording Using 'SU01' 'TestScript.vbs'.

  Wait Up To 30 Seconds.

  PerForm StopRecording.

"-End-------------------------------------------------------------------

"-SubRoutines begin-----------------------------------------------------

"-StartRecording--------------------------------------------------------
Form StartRecording Using TAC Type String FileName Type String.

  Create Object SAPROT 'SapROTWr.SapROTWrapper'.
  Check sy-subrc Is Initial.

  Get Property Of SAPROT 'GetROTEntry' = ROTEntry
    Exporting #1 = 'SAPGUI'.
  Check sy-subrc Is Initial.

  Get Property Of ROTEntry 'GetScriptingEngine' = application.
  Check sy-subrc Is Initial.

  Get Property Of application 'Connections' = connections.
  Check sy-subrc Is Initial.

  "-Connection 0--------------------------------------------------------
  Call Method Of connections 'Item' = connection Exporting #1 = 0.
  Check sy-subrc Is Initial.

  Get Property Of connection 'Sessions' = sessions.
  Check sy-subrc Is Initial.

  "-Session 1-----------------------------------------------------------
  Call Method Of sessions 'Item' = session Exporting #1 = 1.
  Check sy-subrc Is Initial.

  "-Set file name-------------------------------------------------------
  "-
  "-  The file was stored in your SAP work directory
  "-
  "---------------------------------------------------------------------
  Set Property Of session 'RecordFile' = FileName.
  "-Enable recording----------------------------------------------------
  Set Property Of session 'Record' = 1.
  "-Start transaction---------------------------------------------------
  Call Method Of session 'StartTransaction' Exporting #1 = TAC.

EndForm.

"-StopRecording---------------------------------------------------------
Form StopRecording.

  "-Disable recording---------------------------------------------------
  Set Property Of session 'Record' = 0.

  "-Frees all objects---------------------------------------------------
  If session Is Not Initial.
    Free Object session.
  EndIf.
  If sessions Is Not Initial.
    Free Object sessions.
  EndIf.
  If connection Is Not Initial.
    Free Object connection.
  EndIf.
  If connections Is Not Initial.
    Free Object connections.
  EndIf.
  If application Is Not Initial.
    Free Object application.
  EndIf.
  If ROTEntry Is Not Initial.
    Free Object ROTEntry.
  EndIf.
  If SAPROT Is Not Initial.
    Free Object SAPROT.
  EndIf.

EndForm.

"-SubRoutines end-------------------------------------------------------