SAP GUI Scripting and GuiXT
SAP GUI Scripting and GuiXT
1 Requirements
SAP GUI 6.20 and upwards GuiXT 2004 Q2 2 and upwards For SAP kernel 3.1I-6.10: kernel patch required, see SAP note 480149 For SAP kernel 6.20 and above: Included in standard delivery It is necessary to activate scripting support in the SAP system, and in the SAP GUI options Not possible in ITS environment (SAP GUI for HTML)
2 SAP documentation
Please see the following SAP notes concerning SAP GUI scripting: 480149: 587202: 527737: 612454: 619459: Kernel patch requirements Limitations of SAP GUI scripting Composite SAP note on SAP GUI scripting Status and Lifetime SAP GUI Scripting support of SAP applications
The following links provide copies of the original SAP documents, for easy access. SAP GUI Scripting Overview SAP GUI Scripting User Guide SAP GUI Scripting Security Settings SAP GUI Scripting API Help File( .chm)
To display the most recent SAP GUI Scripting documentation, please click on the options icon in the SAP GUI window and select "SAP GUI scripting help"
2.
The internal procedure is as follows: 1. During InputScript processing, SAP GUI receives a new screen description from the application server 2. SAP GUI calls GuiXT. GuiXT reads the script and modifies the screen description. It saves the ApplyGuiScript commands, but does not yet execute the script. 3. SAP GUI builds the full object model from the modified screen description 4. GuiXT calls SAP GUI to execute the specified VBScript 5. GuiXT performs the Enter action
can either: Execute the VBScript outside of SAP GUI by double-clicking on the filename or by entering the name in the MS DOS command prompt Or you "drag and drop" the file into the SAP GUI window
We recommend the 2nd possibility (drag&drop), since in this case you use the same SAP GUI interface that GuiXT uses internally. Some VBScript variables, especially the "session" variable, are already defined and preset by SAP GUI when you use drag&drop.
Save the generated VBScript in a suitable directory, e.g. "C:\guixt\sapgui scripts". The script looks as follows: If Not IsObject(application) Then Set SapGuiAuto = GetObject("SAPGUI") Set application = SapGuiAuto.GetScriptingEngine End If If Not IsObject(connection) Then Set connection = application.Children(0) End If If Not IsObject(session) Then Set session = connection.Children(0) End If If IsObject(WScript) Then WScript.ConnectObject session, "on" WScript.ConnectObject application, "on" End If session.findById("wnd[0]").resizeWorkingPane 139,15,false
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellRow = 2 session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectedRows = "2" In your GuiXT script use "ApplyGuiScript" and specify the name of the VBScript file: ApplyGuiScript "C:\guiXT\sapgui scripts\select_grid_row_3.vbs" The screen will then show up like this:
You can simplify the VBScript if you always use the drag&drop method for testing, since the "session" variable is then defined automatically. The following script does the job as well: Set aw = session.activeWindow() aw.findById("usr/cntlGRID1/shellcont/shell").selectedRows = "2" Please observe that the row index starts with 0 in SAP GUI scripting, so the third row has index 2.
GuiXT script: pos X[GRID1] (2,0) InputField (0,0) "Search for" (0,12) Size=20 name="pm_short_text" Pushbutton (0,34) "Select rows" process="grid_select_rows.txt" InputScript "grid_select_rows.txt": Screen saplslvc_fullscreen.0500 ApplyGuiScript template="select_grid_rows_via_short_text.vbs" Enter VBScript "select_grid_rows_via_short_text.vbs": Set aw = session.activeWindow() Set GRID1 = aw.FindById("usr/cntlGRID1/shellcont/shell") Dim myselectedRows myselectedRows = "" for i = 0 to GRID1.rowCount - 1 if InStr(1, GRID1.getCellValue(i,"KTEXT"), "&V[pm_short_text]", 1) > 0 then if myselectedRows = "" then myselectedRows = CStr(i) else myselectedRows = myselectedRows + "," + CStr(i) end if end if next GRID1.selectedRows = myselectedRows Please observe that, in contrast to the table control, the grid control does not require one to scroll through it page by page. All rows are already accessible without scrolling. You can also scroll the display so that the first selected line is shown: Set aw = session.activeWindow() Set GRID1 = aw.FindById("usr/cntlGRID1/shellcont/shell") Dim myselectedRows myselectedRows = "" for i = 0 to GRID1.rowCount - 1 if InStr(1, GRID1.getCellValue(i,"KTEXT"), "&V[pm_short_text]", 1) > 0 then if myselectedRows = "" then myselectedRows = CStr(i) if i < GRID1.firstVisibleRow or i > GRID1.firstVisibleRow + GRID1.visibleRowCount-1 then GRID1.firstVisibleRow = i
end if else myselectedRows = myselectedRows + "," + CStr(i) end if end if next GRID1.selectedRows = myselectedRows
The scripts look as follows: GuiXT script: pos X[GRID1] (2,0) InputField (0,0) "Search for" (0,12) Size=20 name="pm_short_text"
Pushbutton (0,34) "Select rows" process="grid_select_rows.txt" InputScript "grid_select_rows.txt": Screen saplslvc_fullscreen.0500 Set V[return_filename] "&%[TEMP]\guixtscriptreturn.txt" ApplyGuiScript template="C:\guiXT\sapgui scripts\select_grid_rows_via_short_text.vbs" Enter Screen saplslvc_fullscreen.0500 OpenFile "&V[return_filename]" if Q[ok] ReadFile "&V[return_filename]" pm_selcount CloseFile "&V[return_filename]" Message "I: &V[pm_selcount] rows with short text ""&V[pm_short_text]""" -statusline RemoveFile "&V[return_filename]" endif Enter VBScript "select_grid_rows_via_short_text.vbs": Set aw = session.activeWindow() Set GRID1 = aw.FindById("usr/cntlGRID1/shellcont/shell") Dim myselectedRows myselectedRows = "" Dim mycount mycount = 0 for i = 0 to GRID1.rowCount - 1 if InStr(1, GRID1.getCellValue(i,"KTEXT"), "&V[pm_short_text]", 1) > 0 then if myselectedRows = "" then myselectedRows = CStr(i) if i < GRID1.firstVisibleRow or i > GRID1.firstVisibleRow + GRID1.visibleRowCount-1 then GRID1.firstVisibleRow = i end if else myselectedRows = myselectedRows + "," + CStr(i) end if mycount = mycount + 1 end if next GRID1.selectedRows = myselectedRows ' Generate return file Dim oFS Set oFS = CreateObject("Scripting.FileSystemObject") Dim oTS
sel = Split(GRID1.selectedRows, ",") for each interval in sel lim = Split(interval, "-") if UBound(lim) = 0 then oTS.writeLine(GRID1.getCellValue(interval,"AUFNR")) else for i = lim(0) to lim(1) oTS.writeLine(GRID1.getCellValue(i,"AUFNR")) next end if next oTS.Close
In the toolbar we want to add a new pushbutton that calls up a function from the "object services menu". Since there are no normal menu entries for these special functions, a short VBScript is needed. In this case the VBScript leads to an application server request, so that we have to omit the "Enter" in the InputScript.
GuiXT script: Pushbutton (toolbar) "@FM@Send with note" process="send_with_note.txt" InputScript "send_with_note.txt": Screen sapmv45a.0100 ApplyGuiScript "object_services_1.vbs" VBScript "object_services_1.vbs":