Loading data with Windows mouse and keyboard macro software

Jimbo's picture

Goat typing on laptopTransactions with ALV grids and other Windows-based GUI plugins are out of reach for the traditional batch processing tools provided by SAP. If the transaction has been customized then there is a good chance that the data cannot be loaded with BAPIs or IDOCs provided by SAP.

Getting a developer to update or create tools to load legacy data into these transactions is often harder than getting developer access and permission to change or create the necessary tools. Here is where customized mouse and keyboard macros come into play as a solution of last resort.

Macro software for Windows simulates mouse movements, mouse clicks and keyboard strokes for the purpose of automating tasks and for testing automation. Creating custom macros as text files and launching them from SAP simulates an intern sitting at a computer entering data, but without the complains or errors.

Finding the right Windows-based macro software

Most Windows macro solutions have proprietary file formats that cannot be edited or created in clear text. Furthermore, most do not allow for launching the program or macro from the command line or, if they do, it is only to load the macro into an editor--not to run the macro automatically.

After a great deal of searching, this developer came across JitBit Macro Recorder. This product saves macros as easy-to-edit plain-text files and allows macros to be launched automatically by including the path to the macro file as a parameter of the program along with /a for automatic launch and /c to automatically close the program after the macro has finished.

JitBit wants a little too much for this software; a "Basic" version costs USD39 and the trial version does not support saving macro files or launching them from the command line without first buying a full version. This article is in no way meant to be an endorsement for Macro Recorder and the website is linked here reluctantly, but it does have the two features necessary for this task.

Keystrokes, not mouse clicks

Goat typing on laptopAvoiding unreliable mouse movements and clicks by exclusively using features of the software and keyboard strokes will help to ensure that changes to screen size and resizing of windows will not cause the macros to fail. This whitepaper will not be discussing mouse commands.

Starting with SAPGUI in its beginning screen of "SAP Easy Access" before running the macro is a great way to ensure that the macro software can find the window by title instead of relying on keystrokes or mouse clicks. At the end of the macro, there should be enough F3 keystrokes to ensure that SAPGUI returns to this screen so that the next time the macro is run, it can find the appropriate window without issue.

In the beginning of the macro below, the first lines make Windows focus on the "SAP Easy Access" window and then move the cursor to the Command Field. That means that, while the LSMW is running and launching the macro, another instance of SAPGUI must be open to the SAP Easy Access screen waiting for the macro software to take control.

By using the Ctrl-/ key combination, there is no need to rely on mouse movements and clicks to put the cursor in the Command Field. The transaction code includes the /n in case another program is running in the window, but it should be unnecessary.

Creating and exporting the custom macro is an easy task using an internal table and the GUI_DOWNLOAD function. Here the internal table is defined along with some work areas.

 isLoaded(1) type c,
 nReturn type i.
data : begin of i_text occurs 0,
       text(1024) type c,
       end of i_text.

Populating the table starts with focusing the window on SAP Easy Access, focusing the cursor on the Command Field and then launching the transaction. Then the rest of the fields are populated in their turn.

* Check to see if the recall is already loaded.
isLoaded = ' '.
select * from PNWTYH into lPNWTYH
  select * from WTY_RCLOBJ into lWTY_RCLOBJ
      isLoaded = 'X'.
if isLoaded ne 'X'.  "Not loaded yet.
  refresh i_text.
  i_text = 'SWITCH TO WINDOW : SAP Easy Access : 0'. append i_text.
  i_text = 'DELAY : 200'. append i_text.
  i_text = 'Keyboard : ControlLeft : KeyDown'. append i_text.
  i_text = 'Keyboard : Divide : KeyPress'. append i_text.
  i_text = 'Keyboard : ControlLeft : KeyUp'. append i_text.
  i_text = 'DELAY : 199'. append i_text.
  i_text = 'TYPE TEXT : /nwtyrcl'. append i_text.
  i_text = 'DELAY : 100'. append i_text.
  i_text = 'Keyboard : Enter : KeyPress'. append i_text.
  i_text = 'DELAY : 1999'. append i_text.
  i_text = 'Keyboard : F4 : KeyPress'. append i_text.
  i_text = 'DELAY : 99'. append i_text.
  i_text = 'Keyboard : Up : KeyPress'. append i_text.
  i_text = 'DELAY : 99'. append i_text.
  i_text = 'Keyboard : Up : KeyPress'. append i_text.
  i_text = 'DELAY : 99'. append i_text.
  i_text = 'Keyboard : Up : KeyPress'. append i_text.
  i_text = 'DELAY : 99'. append i_text.
  i_text = 'Keyboard : Down : KeyPress'. append i_text.
  i_text = 'DELAY : 99'. append i_text.
  i_text = 'Keyboard : Tab : KeyPress'. append i_text.
  i_text = 'DELAY : 99'. append i_text.
  i_text = 'Keyboard : Back : KeyPress'. append i_text.
  i_text = 'DELAY : 99'. append i_text.
  i_text = 'Keyboard : F5 : KeyPress'. append i_text. "Create document.
  i_text = 'DELAY : 1111'. append i_text.
  concatenate 'TYPE TEXT :' WTYS-REFNO_RCL into i_text
   separated by space. append i_text. "Reference recall number
  i_text = 'Keyboard : Tab : KeyPress'. append i_text.
  i_text = 'Keyboard : Tab : KeyPress'. append i_text.
  i_text = 'DELAY : 22'. append i_text.
[ Code skipped for brevity . . . ]
  i_text = 'DELAY : 22'. append i_text.
* Now F3 out of the transaction and press Enter when asked to save.
  i_text = 'Keyboard : F3 : KeyPress'. append i_text.
  i_text = 'DELAY : 22'. append i_text.
  i_text = 'Keyboard : Enter : KeyPress'. append i_text.
  i_text = 'DELAY : 1111'. append i_text.
* Press F3 twice more to return to SAP Easy Access
  i_text = 'Keyboard : F3 : KeyPress'. append i_text.
  i_text = 'DELAY : 111'. append i_text.
  i_text = 'Keyboard : F3 : KeyPress'. append i_text.

The internal table is now written out to a custom-fabricated MCR file. This will be file that the macro software loads and automatically runs.

        FILENAME                        = 'C:\TEMP\SAPMACRO.MCR'
        FILETYPE                        = 'ASC'
         CODEPAGE                        = '4310'
        HEADER = '00'
         DATA_TAB                       = i_text
        FILE_WRITE_ERROR                = 1
        OTHERS                          = 2.

Finally, SAP launches a batch file that runs the macro software with the custom-fabricated macro file as a parameter. Then ABAP waits for 15 seconds to allow this macro to complete before starting the next record.

      COMMAND                           = 'C:\TEMP\SAPMACRO.BAT'
      RETURNCODE                        = nReturn.

  "Allow macro to complete before launching another session.
  wait up to 15 seconds.

endif. " isLoaded ne 'X'.

It is a good idea to thoroughly test the execution of the batch file from SAP using the Function Builder transaction (t-code SE37). It's a great place to let SAP know that it can launch this batch file without verification.

Here are the contents of the batch file that SAP calls in order to launch the macro. After the macro program finishes, the exit command closes the CMD/DOS Prompt.

REM ***** This is the batch file that is launched by SAP *****
"C:\Program Files (x86)\MacroRecorder\MacroRecorder.exe" C:\TEMP\SAPMACRO.MCR /a /c