Skip to Content
Author's profile photo Stefan Schnell

How to use SAP GUI Scripting inside Ruby Programming Language

The programming language Ruby offers many possibilities. Also it supports with WIN32OLE class ActiveX automation, and therefore SAP GUI Scripting. The following examples shows, how easy it is to take the recorded VBScript code and to use it inside Ruby.

In the comment lines you see the recorded VBScript code as a direct comparison.

Here the first example which shows the connection to a session, its maximization and the call of the transaction code (TAC) SE16 – Data Browser. As you can see, the VBSCript code was unchanged inserted.

#-Begin----------------------------------------------------------------

require 'win32ole'

#-Sub Main-------------------------------------------------------------
def main

 #Set SapGuiAuto = GetObject("SAPGUI")
  sapguiauto = WIN32OLE.connect("SAPGUI")
  if(sapguiauto == nil)
    return
  end

 #Set application = SapGuiAuto.GetScriptingEngine
  application = sapguiauto.GetScriptingEngine
  if(application == nil)
    return
  end

 #Set connection = application.Children(0)
  connection = application.Children(0)
  if(connection == nil)
    return
  end

 #Set session = connection.Children(0)
  session = connection.Children(0)
  if(session == nil)
    return
  end
  
 #session.findById("wnd[0]").maximize
  session.findById("wnd[0]").maximize

 #session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"
  session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"

 #session.findById("wnd[0]").sendVKey 0
  session.findById("wnd[0]").sendVKey 0

end

#-Main-----------------------------------------------------------------
main

#-End------------------------------------------------------------------

 

The second example shows a complex code. It calls TAC SE80 – Object Navigator – and creates a program, which prints Hello World. And as you can also see, the recorded VBScript could be adopted almost unchanged. The first change was vbCrLf to \r\n, we don’t worry about that because that are different languages. And also it was necessary to add a routine to activate a window, because after the selection of the context menu the script stops.

#-Begin----------------------------------------------------------------

require 'win32ole'

#-Sub activate_window---------------------------------------------------
def activate_window(title)
  wsh = WIN32OLE.new("WScript.Shell")
  while not wsh.AppActivate(title)
    sleep 0.25
  end
end

#-Sub main-------------------------------------------------------------
def main

  sapguiauto = WIN32OLE.connect("SAPGUI")
  if(sapguiauto == nil)
    return
  end

  application = sapguiauto.GetScriptingEngine
  if(application == nil)
    return
  end

  connection = application.Children(0)
  if(connection == nil)
    return
  end

  session = connection.Children(0)
  if(session == nil)
    return
  end

session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE80"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/shellcont/shell/shellcont[3]/shell/shellcont[2]/shell").selectNode "          1"
session.findById("wnd[0]/shellcont/shell/shellcont[3]/shell/shellcont[2]/shell").nodeContextMenu "          1"
#Insert activation of the application window
activate_window("Object Navigator")
session.findById("wnd[0]/shellcont/shell/shellcont[3]/shell/shellcont[2]/shell").selectContextMenuItem "_P__WB_CREATE"
session.findById("wnd[1]/usr/chkRSEUR-WITH_TOP").selected = 0
session.findById("wnd[1]/usr/txtRSEUR-TDPROGRAM").text = "zTest"
session.findById("wnd[1]/usr/txtRSEUR-TDPROGRAM").caretPosition = 5
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/cmbTRDIR-RSTAT").setFocus
session.findById("wnd[1]/usr/cmbTRDIR-RSTAT").key = "T"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[2]/tbar[0]/btn[7]").press
session.findById("wnd[0]/usr/cntlEDITOR/shellcont/shell").insertText "Write: / 'Hello World'.", 11, 1
#session.findById("wnd[0]/usr/cntlEDITOR/shellcont/shell").insertText "" & vbCrLf & "", 11, 24
session.findById("wnd[0]/usr/cntlEDITOR/shellcont/shell").insertText "" + "\r\n" + "", 11, 24
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[1]/btn[27]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press

end

#-Main-----------------------------------------------------------------
main

#-End------------------------------------------------------------------

 

Here an example which opens a new connection:

#-Begin----------------------------------------------------------------

require 'win32ole'

#-Sub Main-------------------------------------------------------------
def main

  application = WIN32OLE.new("Sapgui.ScriptingCtrl.1")
  if(application == nil)
    return
  end

  connection = application.OpenConnectionByConnectionString("/H/NSP/S/3200", -1)
  if(connection == nil)
    return
  end

  session = connection.Children(0)
  if(session == nil)
    return
  end
  
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "BCUSER"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "minisap"
session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"
session.findById("wnd[0]").sendVKey 0

  system "pause>null" #Only to see if it works

  connection.CloseConnection

end

#-Main-----------------------------------------------------------------
main

#-End------------------------------------------------------------------

 

And after all we see that it is very easy to use SAP GUI Scripting inside Ruby. The recorded VBScript code can use almost always unchanged.

Enjoy it.

Cheers
Stefan

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.