-1

I´m dealing with the problem regarding SAP GUI automation. I have a script shown below which is working without any problem BUT it is working only in the first window of the SAP.

When I have something open in the first window of the SAP and for example the home page of the SAP in 2nd window - I will get this error :

800A026B - The control could not be found by id.

Is there any option how to tell: If window 1 is not available try window No. 2 (Or If window No.1 is not available open a new window and perform there the script?)

Error code and its behavior

Script as below:

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 206,40,false
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00082"
session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "/CASWW/LBL_DATA"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[3]/menu[2]").select
session.findById("wnd[1]/usr/chk[2,13]").selected = true
session.findById("wnd[1]/usr/chk[2,13]").setFocus
session.findById("wnd[1]/usr").verticalScrollbar.position = 21
session.findById("wnd[1]/usr/chk[2,13]").selected = true
session.findById("wnd[1]/usr/chk[2,13]").setFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/ctxtI1-LOW").text = "2000"
session.findById("wnd[0]/usr/ctxtI2-LOW").text = "A2C03664400"

session.findById("wnd[0]/usr/ctxtI3-LOW").text = "1.1.2021"
session.findById("wnd[0]/usr/ctxtI3-HIGH").setFocus
session.findById("wnd[0]/usr/ctxtI3-HIGH").caretPosition = 0
session.findById("wnd[0]").sendVKey 4
session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = "20220104"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/ctxtLIST_BRE").text = "2500"
session.findById("wnd[0]/usr/txtMAX_SEL").text = "5000 "
session.findById("wnd[0]/usr/txtMAX_SEL").setFocus
session.findById("wnd[0]/usr/txtMAX_SEL").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[3]/menu[0]/menu[1]").select
session.findById("wnd[1]/tbar[0]/btn[14]").press
session.findById("wnd[1]/usr/chk[1,3]").selected = true
session.findById("wnd[1]/usr/chk[1,4]").selected = true
session.findById("wnd[1]/usr/chk[1,5]").selected = true
session.findById("wnd[1]/usr/chk[1,6]").selected = true
session.findById("wnd[1]/usr/chk[1,8]").selected = true
session.findById("wnd[1]/usr/chk[1,9]").selected = true
session.findById("wnd[1]/usr/chk[1,12]").selected = true
session.findById("wnd[1]/usr/chk[1,17]").selected = true
session.findById("wnd[1]/usr/chk[1,20]").selected = true
session.findById("wnd[1]/usr/chk[1,21]").selected = true
session.findById("wnd[1]/usr/chk[1,21]").setFocus
session.findById("wnd[1]/usr").verticalScrollbar.position = 19
session.findById("wnd[1]/usr/chk[1,14]").selected = true
session.findById("wnd[1]/usr/chk[1,14]").setFocus
session.findById("wnd[1]/tbar[0]/btn[6]").press
session.findById("wnd[0]/usr/lbl[173,3]").setFocus
session.findById("wnd[0]/usr/lbl[173,3]").caretPosition = 0
session.findById("wnd[0]").sendVKey 42
session.findById("wnd[0]/mbar/menu[1]/menu[5]").select
session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select
session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press

session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "D3.XLS"
session.findById("wnd[1]/usr/ctxtDY_PATH").text = "\\cw01.contiwan.com\bdyp\dida4065\Zakaznicka_kvalita\300003300023-0002-0001"

session.findById("wnd[1]/usr/ctxtDY_FILE_ENCODING").text = "0000"
session.findById("wnd[1]/usr/ctxtDY_FILE_ENCODING").setFocus
session.findById("wnd[1]/usr/ctxtDY_FILE_ENCODING").caretPosition = 4
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press


session.findById("wnd[0]").resizeWorkingPane 206,40,false
session.findById("wnd[0]/tbar[0]/okcd").text = "mb52"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtMATNR-LOW").text = "A2C03664400"
session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = "2000"
session.findById("wnd[0]/usr/ctxtWERKS-LOW").setFocus
session.findById("wnd[0]/usr/ctxtWERKS-LOW").caretPosition = 4
session.findById("wnd[0]/tbar[1]/btn[8]").press

Thank you for any advise.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Michal
  • 11
  • 3
  • Your script applies to connection 0 and session 0. The answer is yes, you can choose any connection and any session but I'm not clear what exact logic you want, maybe just checking which session has a given screen number/program open? – Sandra Rossi Jan 06 '22 at 15:18
  • Thank you for the quick feedback. I will try to clarify it in real life situation... You have already opened some transactions in Window 1 and 2 fo example.. Now you need to do something for which you have already created a script (Data downloading from SAP for example) - Because you have already some transaction in the window 1 you are not able to use the script because in any other Window it will fail for mentioned error.. (Or another situation, you have more scripts which you would like to run in parallel but you can´t becase it is running only in Window 1 without issue.) – Michal Jan 06 '22 at 15:49
  • It's what I had understood, but still my question remains, what logic do you want to implement to select the right connection/session, is it okay to select on current screen displayed? (the ID of a screen is program name on 30 characters and screen number on 4 digits) – Sandra Rossi Jan 06 '22 at 18:32
  • Yes, it would be great if it run on the current open screen.. (Not matter if the screen is the No1 or others) Can you descripe me more where exactly should I look for this program name on 30 char. ? It is not part of the script itself right? – Michal Jan 07 '22 at 11:37
  • Via SAP GUI Scripting, screen number is provided by the properties `ScreenNumber` and `Program` of the property [`info`](https://help.sap.com/viewer/product/sap_gui_for_windows/latest/en-US?q=GuiSessionInfo) of the [GuiSession Object](https://help.sap.com/viewer/product/sap_gui_for_windows/latest/en-US?q=GuiSession). You'll probably find many snippets in the Web. – Sandra Rossi Jan 07 '22 at 14:50

1 Answers1

1

Ok, I just solve the issue so the guidance for everyone who would deal with the same problem..

If you have something open in the Window No.1 you have 2 options:

  1. Terminate the script in Window No.1 no matter what has been open there

    For the termination add /n before the name of transaction..

  2. Open a new session and perform the script there.

    For new session add /o before name of the transaction

Use Followigs:

session.findById("wnd[0]").resizeWorkingPane 159,28,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/nName_of_Transaction"
session.findById("wnd[0]").sendVKey 0

So in my code it would be:

session.findById("wnd[0]").resizeWorkingPane 159,28,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/nZQQN_WLQN"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/chkDY_RST").selected = true
session.findById("wnd[0]/usr/chkDY_MAB").selected = true
session.findById("wnd[0]/usr/chkDY_DEL").selected = true

etc...

Thank also Sandra for her help.

Michal
  • 11
  • 3