cancel
Showing results for 
Search instead for 
Did you mean: 

SAPGUI recording through an ABAP program

Former Member
0 Kudos

Can somebody explain how SAPGUI recording can be invoked through ABAP Program. Suppose if I want to record MM01 transaction I need to trigger it through an ABAP program and the recorded data should be downloaded in the form of a file.

In short, the program should work similarly as the sapgui recording.

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

Hello sandeep,

try this:


"-Begin-----------------------------------------------------------------
  Report  zSAPGUIScriptFromABAP.

    "-Includes----------------------------------------------------------
      Include OLE2INCL.

    "-Variables---------------------------------------------------------
      Data SAPROT Type OLE2_OBJECT.
      Data ROTEntry Type OLE2_OBJECT.
      Data application Type OLE2_OBJECT.
      Data connections Type OLE2_OBJECT.
      Data connection Type OLE2_OBJECT.
      Data sessions Type OLE2_OBJECT.
      Data session Type OLE2_OBJECT.

    "-Main--------------------------------------------------------------
      Create Object SAPROT 'SapROTWr.SapROTWrapper'.
      If sy-subrc = 0.
        Get Property Of SAPROT 'GetROTEntry' = ROTEntry
          Exporting #1 = 'SAPGUI'.
        If sy-subrc = 0.
          Get Property Of ROTEntry 'GetScriptingEngine' = application.
          If sy-subrc = 0.
            Get Property Of application 'Connections' = connections.
            If sy-subrc = 0.
              "-Connection 0--------------------------------------------
              Call Method Of connections 'Item' = connection
                Exporting #1 = 0.
              If sy-subrc = 0.
                Get Property Of connection 'Sessions' = sessions.
                If sy-subrc = 0.
                  "-Session 0-------------------------------------------
                  Call Method Of sessions 'Item' = session
                    Exporting #1 = 0.
                  If sy-subrc = 0.
                    Set Property Of session 'RecordFile' = 'TestScript.vbs'.
                    "-Enable recording----------------------------------
                      Set Property Of session 'Record' = 1.
 
 
 
                    "-Disable recording---------------------------------
                      Set Property Of session 'Record' = 0.
                    Free Object session.
                  EndIf.
                  Free Object sessions.
                EndIf.
                Free Object connection.
              EndIf.
              Free Object connections.
            EndIf.
            Free Object application.
          EndIf.
          Free Object ROTEntry.
        EndIf.
        Free Object SAPROT.
      EndIf.

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

to enable and disable the SAP GUI scripting from an ABAP program. It stores the script in the file TestScript.vbs in your standard directory.

Hint: You must disable the security rule for SAPROTWrapper in your SAP Logon, in normal circumstances it is not allowed to create this object.

Hope it works for your case.

Cheers

Stefan

Former Member
0 Kudos

Hi ,

Thanks for your reply , am tryied with your code but it has been hanged.

when i execute the program the recoed has been started after that the screens has been hanged.

I want start and stop the recording when leave the program and at the save a file in background, please find the below code .

REPORT ZSAP_GUI_SCRIPT.

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

CREATE OBJECT saprot 'SapROTWr.SapROTWrapper'.

IF sy-subrc IS INITIAL.

GET PROPERTY OF saprot 'GetROTEntry' = rotentry

exporting #1 = 'SAPGUI'.

IF sy-subrc IS INITIAL.

GET PROPERTY OF rotentry 'GetScriptingEngine' = application.

IF sy-subrc IS INITIAL.

GET PROPERTY OF application 'Connections' = connections.

IF sy-subrc IS INITIAL.

  • Connection 0

CALL METHOD OF connections 'Item' = connection

EXPORTING #1 = 0.

IF sy-subrc IS INITIAL.

GET PROPERTY OF connection 'Sessions' = sessions.

IF sy-subrc IS INITIAL.

  • Session 0

CALL METHOD OF sessions 'Item' = session

EXPORTING #1 = 0.

IF sy-subrc IS INITIAL.

SET PROPERTY OF session 'Record' = 1.

SET PROPERTY OF session 'RecordFile' = 'TestScript.vbs'.

  • SET PROPERTY OF session 'Record' = 0.

FREE OBJECT session.

ENDIF.

FREE OBJECT sessions.

ENDIF.

FREE OBJECT connection.

ENDIF.

FREE OBJECT connections.

ENDIF.

FREE OBJECT application.

ENDIF.

FREE OBJECT rotentry.

ENDIF.

FREE OBJECT saprot.

ENDIF.

please do the need full.

Regards

Sandeep

Edited by: sandeep narsina on Nov 29, 2011 12:13 PM

stefan_schnell
Active Contributor
0 Kudos

Hello sandeep,

you are right, if you use the same session for your ABAP program and for the script recording, the session hangs on the command Set Property Of session 'Record' = 1.

Here is another example, it runs in session 0 and records session 1 for 30 seconds:


"-Begin-----------------------------------------------------------------
REPORT ZSAP_GUI_SCRIPT.

  "-Includes------------------------------------------------------------
    Include ole2incl.

  "-Variables-----------------------------------------------------------
    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'.
      If sy-subrc IS INITIAL.
        GET PROPERTY OF saprot 'GetROTEntry' = rotentry
          exporting #1 = 'SAPGUI'.
        If sy-subrc IS INITIAL.
          GET PROPERTY OF rotentry 'GetScriptingEngine' = application.
          If sy-subrc IS INITIAL.
            GET PROPERTY OF application 'Connections' = connections.
            If sy-subrc IS INITIAL.
              "-Connection 0--------------------------------------------
                CALL METHOD OF connections 'Item' = connection
                  EXPORTING #1 = 0.
                If sy-subrc IS INITIAL.
                  GET PROPERTY OF connection 'Sessions' = sessions.
                  If sy-subrc IS INITIAL.
                    "-Session 1-----------------------------------------
                      CALL METHOD OF sessions 'Item' = session
                        EXPORTING #1 = 1.
                      If sy-subrc IS INITIAL.
                        SET PROPERTY OF session 'RecordFile' = FileName.
                        SET PROPERTY OF session 'Record' = 1.
                        Call Method Of session 'StartTransaction'
                          Exporting #1 = TAC.
                      EndIf.
                  EndIf.
                EndIf.
            EndIf.
          EndIf.
        EndIf.
      EndIf.

    EndForm.

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

      Set Property Of session 'Record' = 0.
      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-------------------------------------------------------

Cheers

Stefan

Former Member
0 Kudos

Hello Stefan,

Thank you very munch for your reply , my problem almost solved with your code .

but i have some modifications in the code based on the client requirement , please help me .

1.I want call Tcode when execute the program (i.e how can use Create Session Method) (i.e Start the SAP Script record feature at a particular point in the transaction )

2.I need to avoid the WAIT XXX Seconds time, I want start record when transaction code is executed and When the transaction ends, stop the recording. In manual mode this is done by clicking the STOP RECORDING button. The complementary functionality to turn off the recording needs to be called when the user attempt to leave the transaction:(i.e Based on SY-UCOMM), how can handle dis ?

3. After done recording the .VBS File is generated but when am trying playback that file its not possible.

How can achieve this , Please do the need full.

Waiting for your reply Stefan......

Cheers

Sandeep

stefan_schnell
Active Contributor
0 Kudos

Hello sandeep,

thanks for your reply.

To your questions:

1. You can create a new session with the following code:

  "-Create a new session------------------------------------------------
    Call Method Of session 'CreateSession'.
    Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.

To get the new session try this before and after you create the new session:

  "-Get all sessions of a connection------------------------------------
    Get Property Of connection 'Sessions' = sessions.
    Get Property Of sessions 'Count' = Count.
    Do Count Times.
      i = sy-index - 1.
      Call Method Of sessions 'Item' = session Exporting #1 = i.
      Get Property Of session 'Busy' = Busy.
      If Busy = 0.
        " With the GuiSessionInfo object you get a lot of technical
        " information about the session
        Get Property Of session 'Info' = Info.
        " One of these informations are the session number, store it
        " in a table and compare the table, after CreateSession, with
        " the new results to find out, which session is the new one
        Get Property Of Info 'SessionNumber' = Number.
      EndIf.
    EndDo.

2. To handle a transaction change you can use this loop:

  Get Property Of session 'Info' = Info.
  Get Property Of Info 'Transaction' = TAC.
  Do.
    If TAC NE MyTAC.
      Exit.
    EndIf.
    Get Property Of Info 'Transaction' = TAC.
  EndDo.

3. Can you publish an example file, it would be helpful to see the problems.

Cheers

Stefan

Answers (0)