0

I tried to run the SAP GUI script recording to gather a text that is displayed for several item numbers automatically. While I was checking it this is the code I got :

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]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "CS03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").setFocus
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").caretPosition = 0
session.findById("wnd[0]").sendVKey 4
session.findById("wnd[1]/usr/lbl[1,10]").setFocus
session.findById("wnd[1]/usr/lbl[1,10]").caretPosition = 0
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/ctxtRC29N-MATNR").text = "508546"
session.findById("wnd[0]/usr/ctxtRC29N-WERKS").text = "1000"
session.findById("wnd[0]/usr/ctxtRC29N-WERKS").setFocus
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tblSAPLCSDITCALT/ctxtRC29K-STLST[1,15]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/tblSAPLCSDITCALT/ctxtRC29K-STLST[1,15]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCDO").select
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA").select
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-KTEXT[3,3]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPDAT").select
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-KTEXT[3,6]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPDAT/ssubSUBPAGE:SAPLCSDI:0840/btnRC29P-ICON1").press
session.findById("wnd[0]/usr/cntlSCMSW_CONTAINER_2102/shellcont/shell").setDocument 1,"e1xydGYxXGFkZWZsYW5nMTAyNVxhbnNpXGFuc2ljcGcxMjUyXHVjM="

Obviously, I did not get the required result as I want to retrieve the displayed text and not the text field. Any ideas on how to get that?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
MopMop
  • 119
  • 2
  • 14
  • I am afraid but you have to be more specific. The code above seems to be recorded with the script recorder and does not contain the transaction you are calling. There is no line like session.findById("wnd[0]/tbar[0]/okcd").Text ="/NVA03" or whatever transaction. And I do not understand what you mean with displayed text. You get the content of a sapgui field with .text, for example session.findById("wnd[0]/usr/ctxtEKKO-LIFNR").Text. You can find out the name of the fields with the Scripting Tracker https://tracker.stschnell.de/index.htm – Storax Jun 01 '17 at 14:18
  • @storax I have just edited it :) – MopMop Jun 06 '17 at 06:33

1 Answers1

1

First of all instead of .setFocus method try to use .Text method. Then create variable like strMyText. After that assign text from SAP to this variable and at the end paste it somewhere. So it will be something like this:

Sub SAPText()

Dim strMyText

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]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "CS03"
session.findById("wnd[0]").sendVKey 0

strMyText = session.findById("wnd[0]/usr/ctxtRC29N-STLAN").Text

Activecell.value = strMyText

End sub
SuShuang
  • 190
  • 6
  • Hey @SuShuang I have another question. The data displayed there is on three different lines. When I copy it in my excel sheet it goes in a cell but doesn't keep the formatting. However for this task the formatting is very important. Any idea on how I could do that ? – MopMop Jun 08 '17 at 12:27